Python no longer launches from command prompt after windows update - python

My Windows 10 computer just installed updates and now when I go to the command prompt and type "python" the Windows store opens with a download option.
My path already includes two versions of Python for some reason:
C:\Users\Josh\AppData\roaming\Python\Python37\
C:\Users\Josh\AppData\Local\Programs\Python\Python37
Any suggestions? I can type in a python script (think.py) and it runs as expected, but python think.py does nothing.

python should work in cmd if Python is correctly installed and added to Windows path. py also exists and allows for a specification of a version i.e. py -2 or py -3. In your case it appears that python is installed in appdata and not program files, and this is most likely the issue with the python command not working! Here is a pretty comprehensive article on the differences between the two!

Related

How do I access updated Python 3.9 in VS Code via command line?

I just downloaded VS Code this week and have run into a problem accessing different versions of Python.
When I run a python file called set.py from the command line via python set.py, VS Code is accessing the Python 2.7 version that came with my MacBook. When I run python --version it confirms that VS Code is using Python 2.7.16.
However, when I right-click in the editor and select Run Python File In Terminal it appears to be using the updated version 3.9.4. I have tested this by using an fstring (available in 3.9 but not in 2.7). The fstring code throws an error in the first method but clears fine in the second. Here's my code:
s = set()
s.add(1)
s.add(2)
s.add(3)
print(f"The set has {len(s)} elements.")
And here's my terminal with the results of both methods shown.
Does anyone know what exactly is going on, and how I can execute python files via the command line?
TIA
Specs:
I am running VS Code 1.55.2 and have downloaded Python 3.9.4 to my MacBook Pro running Big Sur 11.2.3
I have also downloaded the Python extension in VS Code and have selected Python 3.9.4 64-bit as my interpreter.
On macOS and linux, you will have a system python that tends to be old and you don't want to touch it because there are system utilities that rely on it.
You will also have the user-installed python, which here is python 3.9.
If the system python is python2, then the command python will typically invoke python2 and the command python3 will invoke python3.
In VS Code on Windows, when you launch a terminal window from the IDE, it will activate the interpreter/environment you've chosen for your project (typically by CTRL-SHIFT-P and then Python: Select Interpreter). I've noticed on Linux that it will not do this (and this may also be the case on macOS), so if I want to use a specific version or environment, I need to specify it or activate it as my first command in the terminal with something like conda activate myenvname.
Unlike in the terminal, VS Code will use the selected interpreter if you run code directly from the IDE (using Run Without Debugging or Control + F5).

Python3 seems not to be recognized in VSCode

I had Python version 2.7.17 already installed on my machine. Recently I started coding in Python and I installed VS Code to make things easier. However once I opened VSCode and ran a simple Python Hello World program, VSCode terminal gave a warning saying that
Python 2 support has ended at January 1, 2020
which means the support has already ended and suggested to install the latest Python version. So I heeded the warning and installed Python 3.8.5 on my Windows machine. The installation was completed without any issue. Afterward, when I tried the command python --version on VSCode terminal, it correctly gave the output python 2.7.17. However, when I tried the command python3 --version to check if it was correctly installed, the terminal gave no output.
Since I am a beginner and the support for Python 2 has already ended, I would like to work with Python3 from now on. However, I don't mind Python 2 staying in the machine.
Is this something should I worry about? Or should I uninstall Python 2 completely (When I checked Control Panel both versions are list under currently installed programs)?
According to your description, you can change pythonpath in vscode and still keep python2.
I assume you are using windows system and if not, please let me know. The following is the solution:
open cmd and type: where python. There should be two path, copy it which is about python3.8.5;
press Ctrl+, to open setting.json and add an entry for python.pythonPath manually inside your User Settings:
Now you can press Ctrl+Shift+P and find the option select interpreter. Click it you'll find a interpreter list. After selecting new interpreter, it is necessary to press Ctrl+Shift+` to open a new terminal for further development.
About more detailed information about pythonpath and interpreter you can refer to using python environments in vscode.
The version of Python used depends on your environment, and PATH variable.
I think you need to select the correct Python interpreter. At the bottom of your screen, there should be something that says the Python version number. If you click it, you can get a menu to select the interpreter.
Alternatively, you can hit control-shift-p, type "python interpreter", then click "select python interpreter" option.
Finally, you can alter the default Python interpreter used by changing the User settings in VS Code. Hit control-shift-p, type "open settings", and select Open Settings (JSON). Add a key to the JSON file called "python.pythonPath", so that you have a line that looks something like this:
"python.pythonPath": "full/path/to/the/python/executable/you/want/python.exe",
You might be able to get a hint as to the path to use or available isntallations of python if you type where python or which python (depending on terminal/ OS in use).
Then you can save those settings. Restart VS Code. Hopefully now the correct interpreter is selected by default.

Anaconda on Windows 10: iPython and Spyder fail to start in Python3 environment

I'm using Windows 10 and I have Anaconda with Python 2 installed, so my root environment is Python 2. I created an additional Python 3 environment and among other packages installed iPython and Spyder into it. I used the Anaconda Navigator to install the packages.
I can activate and deactivate the environment using Windows CMD just fine. After activating the Python 3 environment in the CMD the ipython command typed into the same CMD starts up Python 3.6.1.
The Anaconda Startmenu folder does contain shortcuts to iPython and Spyder both for Python 2 and Python 3 now. I can use those to start both for Python 2 as before, but the Python 3 versions won't start. And there is no error message or crash or anything.
When clicking on the Python 3 iPython shortcut a command prompt pops up for a split second and immediatly closes again. Spyder does not even open a command prompt, it does absolutely nothing, I presume it's because iPython fails. Checking the task manager shows that there is no Python running in the background at all, so it really does not start.
Now I know that iPython 3 itself is not broken because I can start it from within CMD after switching environments, nonetheless I deinstalled and reinstalled them both, no change.
I then went into the shortcut to get the exact command it was executing to write a small batch file with a pause command to see if anything gets displayed when iPython fails. Doing a right-click on the shortcut and executing "open file location" leads me to python.exe in the Python 3 environment base folder, and executing that works fine of course.
So now I'm stumped since I have no leads to solve or even analyze the problem properly, over the entire course of action not a single error message ever appeared anywhere.
Any hints and suggestions are appreciated.
EDIT:
The target of the Python 3 shortcut in the properties looks like this:
C:\Users\My.Name\AppData\Local\Continuum\Anaconda2\envs\Python3\python.exe C:\Users\My.Name\AppData\Local\Continuum\Anaconda2\cwp.py C:\Users\My.Name\AppData\Local\Continuum\Anaconda2\envs\Python3 "C:/Users/My.Name/AppData/Loca
The working shortcut to Python 2 looks pretty much the same:
C:\Users\My.Name\AppData\Local\Continuum\Anaconda2\python.exe C:\Users\My.Name\AppData\Local\Continuum\Anaconda2\cwp.py C:\Users\My.Name\AppData\Local\Continuum\Anaconda2 "C:/Users/My.Name/AppData/Local/Continuum/Anaconda2/pyth
I found the problem.
As it turns out the module menuinst wasn't automatically installed into the new environment so I had to manually install it. Now everything works.

Python 3 installation on windows running from command line

Just curious, is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux? Is there some kind of way to configure Python so that it runs like this? Thanks.
EDIT: Just to add, the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".
the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".
To run Python 2 executable:
C:\> py -2
To run Python 3 executable:
C:\> py -3
where py is a Python launcher that is bundled with your Python 3 installation.
py recognizes the shebang (e.g., #!/usr/bin/env python3 causes Python 3 executable to be run), it respects virtualenv (if you run py without specifying the explicit python executable version) i.e., run:
C:\> py your_script.py
and the correct python version is used automatically -- you don't need to specify the Python version on the command-line explicitly.
is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux?
OSX and Linux have python executable installed by default as a rule and it refers to Python 2 version in most cases at the moment that is why you need a separate python3 name there.
There is no Python on Windows by default. And therefore any version that you've installed is just python (I guess). The recommended way to manage multiple python versions is to use the Python launcher.
Is there some kind of way to configure Python so that it runs like this?
If you want to type python3 some_script.py instead of py some_script.py or even just some_script (assuming .py is in %PATHEXT% and Python launcher is configured to run Python scripts (check assoc .py and ftype Python.File) -- the default) then create a bat-file e.g., python3.cmd and put it in %PATH%:
"C:\path to\Python 3.X\python.exe" %*
You likely missed the checkbox at the bottom of the installer.
Full documentation here: https://docs.python.org/3/using/windows.html
Then, I think you just run python, not python3 from the Command Prompt. The reason Unix systems have python3 is because python defaults to Python2.x in many systems.
You have to add the python bin folder to your path. You can do it manually but when you install python i remember you have an option to do that.
I work with multiple Python 2.x and 3.x distros on Windows. Some of them are "portable" - i.e. not recorded in the Windows registry, and therefore not accessible by the version-selector py.exe delivered with Python 3.3+. To save my sanity, I wrote SelectPython.bat which is available on bitbucket. It configures the PYTHONHOME, PYTHONPATH and PATH variables according to the target you give it (a relative or absolute path to the parent directory of python.exe). You can do so in a way that is sticky for the rest of your command-line session:
> SelectPython C:\Path\To\Desired\Version\Of\Python
> python
or transiently, i.e. to call a particular python command without otherwise affecting the environment of the shell you're calling it from:
> SelectPython C:\Path\To\Desired\Version\Of\Python python -c "import sys;print(sys.version)"
You may find it helpful.

How to make Python 3 my default Python at command prompt?

I have uninstalled Python 2.7 and installed Python 3. But, when I type Python on my command prompt I get this :
"Enthought Canopy Python 2.7.9 ........."
How can I run Python 3 from command line or how can I make it default on my computer? I asked Enthought Canopy help and I was told that I can "have Canopy be your default Python only in a "Canopy Command Prompt". Not sure what it means.
edit : Thanks everyone. As suggested, I had to uninstall everything and install Python again.
Windows selects which executable will be run by searching the directories in-order from the PATH environment variable (source):
The shell now searches each directory specified by the PATH
environment variable, in the order listed, for an executable file
matching the command name. If a match is found, the external command
(the executable file) executes. If no match is found, the shell
reports an error and command processing completes.
You can modify PATH to put your Python 3.4 directory (typically C:\Python43) before your Python 2.7 directory. From Windows 7 and up, you can use where python to confirm which python will be run (first line of the output).
After editing each path and creating a new variable for each python version, be sure to rename the python.exe to a unique one. i.e. "python3x" . then you can call it in the command line as "python3x". I am assuming that the original python installed (2X) retains the python.exe of which when you call "python" in the command line, it will show the 2x version
You can copy python.exe to python3.exe.
If you are using Anaconda, then you will find it in the sub directory of your environment, for intance, c:\Anaconda\envs\myenvironment.

Categories