CMD redirects PY code to new window that disappears

When my code asks for an input of a web site for scrapping, CMD launches a new window. The new window disappears before I can read information.

I am a newbie learning Python by taking an online course, Python for Everyone by Dr. Chuck. Running programs from CMD worked perfectly. Then I did something attempting to install beautifulSoup4. It is 100% me and not the the package or python but is preventing me from continuing with the course.

The new CMD window has the path to the Python source code. There were online comments to create a path in environmental variables. I have now uninstalled that path. How to I keep python in the same CMD window?

I have tried uninstalling and reinstalling Python based on a forum that suggested the program launches a new window in CMD. I have checked for viruses and run scan.

Please help.

1 Like

What happens if you write py urllinks.py in the CMD window instead of just urllinks.py?
What happens if you write assoc .py? (For me it prints .py=Python.File)
What happens if you write ftype Python.File? (Or replace Python.File with the respective output you got from assoc.) (For me it prints Python.File="C:\WINDOWS\py.exe" "%L" %*)

(This means when I type urllinks.py into CMD, it will notice it’s a .py file, which is associated with the file type Python.File, which can be executed using the py command. So it runs as if I had typed py urllinks.py.)

Maybe on your system these associations are somehow different and include a step that opens a new CMD window?

1 Like

Peter, you are the man. I thank you very much.

I can now see my errors with the urllinks.py program so I can begin trouble shooting. Running py urlinks.py worked. My other replies are the same as yours. If you have ideas on what to check the system associations, I would appreciate it.

I can not thank you enough. I had spent a frustrating 2 days trying to see my file. I think you deserve extra turkey and a second helping of dessert.

Happy Thanksgiving,
Ed

2 Likes

Here is a screen shot.

Glad it helped. (I don’t know why omitting py doesn’t work for you then. But I always write py myself anyway.)

A guess about the problem other problem visible there: Could it be you have a very old version of beautifulsoup4? It still uses collections.Callable but that has been moved to collections.abc.Callable a long time ago, and bs4 seems to have adapted for that for some years.

CMD’s assoc and ftype commands are outdated. They were introduced back in Windows NT 4.0 (1996) and haven’t been significantly updated since then. They only show and modify the system software classes in “HKLM\Software\Classes”, for a given file type (e.g. “.py”) or given programmatic identifier (e.g. “Python.File”). A lot has changed since 1996. File associations can be defined in many places in the registry:

  • HKCU\Software\Classes
  • [HKCU|HKLM]\Software\Classes\Applications
  • [HKCU|HKLM]\Software\Classes\SystemFileAssociations
  • [HKCU|HKLM]\Software\RegisteredApplications – “Capabilities” key paths
  • [HKCU|HKLM]\Software\*\Capabilities\FileAssociations

More importantly, the shell API’s calculated associations and user choices are found in “HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts”.

If you have admin access, using ftype to set a file association to a given progid at the system level may work sometimes, since the shell will recompute the file association, and the new file association might take precedence. But that’s not necessarily the case, and certainly it’s not the case if the user has locked in some other preferred application (e.g. “always use this app to open …”).

If the py launcher is effectively associated with “.py” files, then in the settings for “Apps -> Default apps”, the “.py” file type will be associated with a “Python” app that has a rocket on its icon. If it’s not, click to modify it. If the dialog to select another app doesn’t include a “Python” app with a rocket on its icon, then something is configured incorrectly.

2 Likes

FYI, “cmd.exe” is a command-line shell that uses a console or terminal for its user interface (UI). A console or terminal provides a display or window (or a tab in a window) for text output and reads keyboard input (sometimes also mouse/touch input). On Linux, I use GNOME Terminal and the system’s builtin virtual terminals (e.g. Ctrl+Alt+F3). On Windows, a console is hosted by a separate application – either builtin “conhost.exe” or, for Windows Terminal, “openconsole.exe”.

The CMD shell uses a console, not any different from how “py.exe” and “python.exe” operate. If you run “python.exe” or “cmd.exe” from Explorer, the system sees that it’s a console application and automatically allocates a new console for it. When a console has no more attached client processes, it gets automatically destroyed. If it’s the builtin console (conhost.exe), this destroys the window as well, which can be frustrating. Windows Terminal can be configured to leave the tab open after an “openconsole.exe” session has ended.

A shell is a command-line interface (CLI) application that provides a set of commands, operators, and usually a scripting language. Examples include bash, CMD, PowerShell, and even Python’s interactive shell (i.e. the REPL, a Read-Eval-Print Loop). When interactive, a shell displays a prompt (e.g. ">>> ") and waits for the user to enter a command. System shells such as CMD are designed to facilitate running and scripting applications.

Python’s builtin shell is designed for interactive testing and experimenting, but it’s pretty basic. More capable Python shells exist, such as IPython. Related to that, Jupyter Notebook is another popular environment in which to use Python, particularly for scientific research and data analysis. If you’ve used Mathematica before, it’s like that.

1 Like

Peter,

Everything worked fine until chapter 13 in my course. The problem is something I have done to the configuration and not beautifulsoup. I am new to programming and when BS would not run, I googled the issue and started playing with the configuration settings. Lesson learned, don’t follow random Internet advice but look for answers on this forum.

Thanks for your help.

Hi Eyrk,

It is going to take me about three weeks to digest and understand all the information you have given me. How did you learn so much? I was able to follow your suggestion and go to App-> Default Apps in Windows 11.

It worked. I changed from “Python 3.1 with the bubble/squares symbol” to “Python with the Rocket ship”. Now, when I run my code it stays in the same CMD shell.

I appreciate you and your friendship.

Happy Thanksgiving.
Ed