So, I've been toying around with python environments and I think i screwed mine up.
So when I ran python in shell, it would tell me i'm running on 2.7
I'm on windows 10, and i need a python switcher for my next project so i download pywin and used pywin to switch it to 3.5
the command i used was
pywin setdefault 3.5
now when i type python it says
python is not recognized as an internal or external command.
but py produces
Python 2.7.12
Now i can't use pip, easy_install, virtualenv
all of these commands i used to use, i suddenly no longer have access to them.
I tried switching back
but it wont even recognize pywin anymore.
The best way to check which Python version is executed is to check your environment variables. Another way of checking this is using the which command. (open cmd and run which python).
But, first you need to start a new cmd prompt to ensure your environment variables are not altered.
On Windows, but also any OS, you need to check the PATH and PYTHONPATH variables.
For Windows, follow the recommandations available in the Python documentation.
If you're not very experienced with working with windows, installations, and other similar things, I would recommend that you uninstall python, delete all versions/folders containing python (compiled) files (those that were installed with python, not the ones you've written) and then reinstall python. The installer should re-set the path variable to the correct location.
Related
When I put "py --list" in the terminal I get this:
Installed Pythons found by C:\Windows\py.exe Launcher for Windows
No Installed Pythons Found!
I set my environment variables. I actually got VS Code to run some code I threw together that didn't use any imports, but now that I'm trying to use any python commands in the terminal, I'm getting errors. What gives?
python should work, since it is finding installations.
If you are working on Windows, very often it's all about permissions. Try running your terminal and VSCode as administrator! Worked a lot of times for me, also with other languages.
Please ensure that you have installed the python extension and selected the correct python interpreter (Ctrl+Shift+P and type Python: Select Interpreter).
If this didn't work, you may have to reinstall python.
Read the document about Vscode-Python for more details.
I'm using vscode on macOS and using nix to manage my environment. I do have python3.9 installed via homebrew as my system-default python, but I'm working on a project using a nix environment that is setting my path such that the only python executable available is python 3.8.
$ which python
/nix/store/mr2p1ycgj3wfppbw8ry2wwxg9r0xjz0z-python3-3.8.8-env/bin/python
$ whence python
/nix/store/mr2p1ycgj3wfppbw8ry2wwxg9r0xjz0z-python3-3.8.8-env/bin/python
$ whence python3
/nix/store/mr2p1ycgj3wfppbw8ry2wwxg9r0xjz0z-python3-3.8.8-env/bin/python
$ code .
The VSCode instance that comes up finds my 3.9 install as the default interpreter...
This causes a problem because the interpreter that's put on the path by nix is also the one with all my packages installed for this particular project, including developer utilities like black, flake8, pylint, etc... and my default 3.9 install does not necessarily have those.
It seems like there's a mechanism by which the Python extension discovers various python installs (I see in the extension output things where it tries to source virtualenvs and query pyenv), so what I think is happening is that it's finding a bunch of different python interpreters and blindly defaulting to the newest.
Is there any way to override the way the Python extension discovers python installs? I just want it to use the python interpreter that it finds on the path by default. I know I can override the interpreter per-project via the .vscode/settings.json, but that's a bit tedious to do for every project, and the way nix works that path may change occasionally. I already have "python.pythonPath": "python" in my vscode settings (which is the default anyway) - I was hoping that would work... it does not.
When we use "code." to open VS Code from the system terminal outside VS Code, it uses the latest version (highest version) of python installed in the system by default.
For directly opening an existing folder (containing python files) from VS Code, it defaults to using the python environment used last time, and for a new project that has not been selected, it also defaults to using the latest version (highest version) of python installed in the system .
You could click on the python environment in the lower left corner of VS Code, and then switch to the desired python environment in the options. In addition, it is recommended that you open the previously used project from VS Code (click File, Open Folder) and use the virtual environment in VS Code.
I have windows 10 with 2 user accounts, one is an old one that has Python installed. My latest account also has Python installed. I used pip install tweepy, and it now exists in the site packages. If I open my Python shell (v3.6.5), it imports fine. However, I can't get this to work when switching to Python in cmd. In here, after typing python, it says Python 3.6.6. Obviously they are different, yet they seem to be originating from the same directory.
Any ideas?
[edit] I have just realised that I have an external application that runs on Python 3.6.6., so I guess it's defaulting because of that. However, I can't uninstall that since I need it - is there a way to specify which version of Python is launched when I type python into the cmd?
[edit2]: This is the issue, and the solution there works. However, I want to be able to just type python into cmd, rather than the entire path. Since both exist in the path (and I don't want to remove the other 3.6.6.), is there a way to achieve this?
Have you thought about using a virtual environment?
https://virtualenv.pypa.io/en/latest/
When you enter "python" into your cmd it searched python.exe in the directories listed on your PATH variable.
All you need to do is to modify your PATH and add the directory path in which the python with the desired version is located.
I am trying to use Anaconda and conda environments to allow Python programs for data acquisition* etc. to run from the (Anaconda) command line on Windows. The set up will be that the Python programs are installed to a particular location (cloned from Github), within %PATH% or whichever environment variable is more appropriate.
From an Anaconda command prompt in another directory and a particular conda environment, I want (both myself and other users) to be able run either python test.py <args> or test.py <args> (either solution is acceptable) and have a system wide conda environment run its Python to execute the program. test.py can/will have an appropriate shebang set.
Right now the python test.py calls the correct Python within the active conda environment, but cannot find the test.py program as Python won't search the %PATH% or similar looking for the program. test.py does something (Windows does not complain that the executable can't be found, and I've been playing with the file associations to get this far), but doesn't appear to start Python - a simple print function or raise statement as the only entry in the file does nothing.
I've tried setting file associations in Windows, but this hasn't changed anything. I've copied the py.exe/pyw.exe across to the Anaconda environments, with no change.
Is this something that can be done within Anaconda, or am I going to have to fall back on installing base Python directly and trying to use the launcher mechanism there?
Note that I'm also intending to deploy these programs on Raspbian, so any solutions, including non-Anaconda ones, that will work cross platform there would be worth extra effort on my part.
*these programs have significant usage of library packages for accessing external USB/GPIB/serial/ethernet connected lab equipment and use matplotlib, scipy, etc., hence the desire for a cloneable conda environment as the base environment.
It turns out the correct answer to this is fairly simple, but is fairly hard to find explained well. This might be a little clearer than the other answers I found:
Install the standalone launcher from pylauncher and add #!/usr/bin/env python shebangs to your scripts.
This should register .py files to Python.File and will find your Anaconda Pythons in appropriate environments. If you don't have a non-Anaconda python, it will use the Anaconda base environment (these two facts were the key element I was missing from various other answers around this problem that I had looked at, including the documents on python.org).
If you have a Python from python.org installed, then a standalone command line shell will use that, defaulting to Python 2.x, then Python 3.x. With #!/usr/bin/env python shebang, then a regular command shell will try to use python.org pythons first, then the Anaconda base environment. An Anaconda prompt will use the active environment. #! /usr/bin/env python2 or python3 will try to use python.org pythons only and fail if they are not found.
Installing Python 2.7 from python.org installers (and letting the installer set the file associations) will break pylauncher, and reinstalling will not fix it. Instead, set Computer\HKEY_CLASSES_ROOT\Python.File\Shell\open\command default value to "C:\WINDOWS\py.exe" "%L" %* to revert back to the pylauncher set up (assuming you used the launchwin.* packages to install it).
I currently have python 2.x and 3.x installed on my windows machine, id like to have both for comparison and also 2.x for pygame.
now i know if i want to run a python script in the command prompt i would use the following:
python filename.py
i know java also uses the "java" prefix before it if you want to run a java file.
Now in doing this, it automatically sets up python 3.x environment, which i dont mind, but i was wondering is it possible to set up 2.x enviroment by setting a variable called "Python2" and another for python 3.x such as "Python3" so that i could set up the environment without having to use the trick of adding the environment each time in the .py script?
this might be a silly question, but im not overly familar with command prompt's enviroment variables and its keywords, i did try googling it but alot of people just ended up suggesting either env path in the file or manually change the enviromental path each time.
Update
My machine is running windows 8.1
Follow this tutorial (upto step#4):
http://www.computerhope.com/issues/ch000549.htm
Then click on NEW -> add the 2 paths with their call names
Python2,
Python3,
While doing this, you may want to add all these to PythonX path :
C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;
C:\other-foolder-on-the-path (other folders)
After snooping about SO, i eventually found my answer, i guess it never came up in my initial searches for some reason
How to switch between python 2.7 to python 3 from command line?