I have 3 versions of Python (2.5, 2.7, 3.2) installed on a Windows machine, 2.5 being the default one (first in PATH and default for open action).
Now the weird thing appear when I run a python script with filename.py (without specifying the interpreter) or by clicking the file in Explorer: Python 2.5 is running the script (expected) BUT Python 2.7 PATH being inserted before the original system PATH.
Still if I look at the command line, it seems that Python 2.5 was executed.
Where is the first record in PATH (C:\Ptyhon27\) comming from, I can assure you this is added when the scripts runs, but by whom?
As you already observed I do have several versions of Python in PATH, this is not something anormal, because they can also have versioned executables instead and because Windows always picks the first one it the PATH.
Looking closely, it seems you have various versions of Python in your PATH environment variable.
The standard Python installer for Windows doesn't add itself to the PATH; I always do this manually for the version of Python I want to use by default. If you're using a different Python installer (such as ActiveState or Enthought) that may be the cause, but I haven't tried those.
In any case, you can edit your PATH environment variable manually and clean it up, leaving only the path to the version of Python you wish to be the default, as described here (scroll down a bit to get to the relevant section).
You might be interested in the Python Launcher for Windows project, a.k.a. PEP 397. Install it, and remove all Python dirs from PATH, leaving only the launcher one, and use py/pyw instead of python/pythonw.
Ahh, I think I have it! You didn't mention exactly how you're running Python...
Python adds the directory from which the command is run to the PATH! If you run it from the command line, this will be the directory in which the Python file you execute is located, or the directory you ran Python from if you just opened an interpreter. However, various tools, interpreters and development environments start up differently, and some of them will use a certain Python interpreter (depends on their configuration) and add its location to PATH.
If you want more help, please give a detailed description of how you're running Python when this happens.
Related
we have a bunch of code in python - we have a sort of bootstrap bash script that kicks off various things - and we are trying to just set a python_bin variable that points to python 3. I tried this:
if [[ $( which python 2>/dev/null ) =~ /Python3 ]]; then
python_bin=python
elif which python3 >/dev/null 2>&1; then
python_bin=python3
else
python_bin=python
fi
but on some windows boxes it's not finding the right python because it's behind some runner "py". Is there a definitive way in bash to get to python3 that works on any platform where python and bash are both installed in the path?
Basically we want someone to be able to get clone our repository and run setup.sh - and keep finding environments where the python install is different in some new and interesting way.
Is there a definitive way in bash to get to python3 that works on any platform where python and bash are both installed in the path?
First off, are you sure that's sufficient? There could be any number of Python installations within folders that are on the PATH. For that matter, are you sure that your script will run correctly even under 3.0?
Second, no, you can't ever do this in a completely foolproof way. For one thing, Windows users are under no compulsion to leave python.exe's name alone. After all, nothing in Windows is dependent upon it. Linux and Mac users even have the same freedom for other installations besides the system one. For another, just because a system has Python installed doesn't mean it has any 3.x installations.
(Oh, and you should probably take a moment to make sure you understand how virtual environments work, and do some testing with those. Depending on the platform and the user's preferences, these might either copy the base Python environment or symlink it. Then there are all the alternate implementations: Jython, Pypy, IronPython....)
On the other hand, 2.7 has been EOL for over two years; it is approximately as old as Windows 7. Your users should be expecting any new code to demand 3.x anyway - and a reasonably up-to-date version at that. Even 3.6 is EOL now.
some windows boxes it's not finding the right python because it's behind some runner "py".
Keep in mind that if you are using Bash on Windows then you are effectively reliant on hacks anyway. I would recommend writing a separate .bat version of the bootstrap, if possible.
The Python launcher for Windows exists because Windows does not have a built-in concept of shebang lines. Not everyone on Windows uses it. The entire point of py is that it circumvents the PATH environment variable; py itself is on the path (typically in the Windows install directory, for global installations), and it maintains its own registry. It does this exactly in order to support more complex logic for choosing an executable; using PATH can only ever give you the first valid thing that was found. Systems that use py will generally not put any of their installed Python installations on the PATH, because py will take care of it. But they could, and who knows which one will be found first.
If you really want to stick with this detection logic, try prioritizing a search for py, and use py -3 if py is available. But...
Basically we want someone to be able to get clone our repository and run setup.sh - and keep finding environments where the python install is different in some new and interesting way.
The best you can do is to just write a shebang line, set the executable bit for the script on systems that support it, use the Bash script to run these executable scripts as scripts (rather than trying to find the Python executable), and let users be responsible for their own environment variables. People who set up their environments in weird ways are supposed to be responsible for understanding the weirdness. I appreciate the desire to try to provide a turn-key solution for people who might not be that technically proficient - but if your audience includes the sort of person who installs Bash on Windows, there are at least some reasonable guarantees on technical proficiency.
On ordinary Linux and Mac setups, a shebang like #!/usr/bin/env python3 should give you the default 3.x Python in the current context. Such a shebang is also recognized by py, which will parse it in a completely different way: the .py file extension is registered to be run by py; py sees the shebang and applies some regexes; it sees that the command includes python3, and runs whichever Python it has registered as the default 3.x installation.
When I start Pycharm, this python setup menu pops up.
Python 3.7.4 is not even the version I'm using. Everything else works fine and I can just close it, but it is annoying.
There was a same question, and the answer was
You appear to be using the installer .exe as if it were the Python interpreter. Install Python to somewhere first, then use the python.exe in the bin directory inside that.
But I can't find where my bin directory is. My Python37-32 folder looks this.
How do I find my python interpreter?
Open some project -> File -> Settings -> Project: ... -> Project Interpreter -> Show All ... to view the list of available interpreters added to PyCharm.
Find and remove the interpreter which is pointing to Python distributive from the newly opened list.
At some point in past you added Python installer as a project interpreter in PyCharm so now when IDE tries to use this exe to run some background tasks (e.g. to get sys.path) installation wizard is triggered instead.
Correct python.exe to point PyCharm to is right on your screenshot
According to official python documentation.
The Python interpreter is usually installed as
/usr/local/bin/python3.8 on those machines where it is available;
putting /usr/local/bin in your Unix shell’s search path makes it
possible to start it by typing the command:
Since you are using Windows here, you are already in the location where the python interpreter resides. So look no further.
By the looks of the screenshots, I have a concern whether you have installed python correctly.
If you did not select the checkbox for Add Python 3.7 to PATH, the wizard expects you to do it manually which is why the error.
I would uninstall and reinstall Python and this time make sure to check that box. I am sure your error messages should go away.
Alternatively, if you're not in the disposition to uninstall. According to python docs here you can set the python variable to PATH variable and try.
I'm reinstalling Python, on Windows 7, and one of the first dialog boxes is the Customize Python screen.
The default setting for "Add Python.exe to Path" is "Entire feature will be unavailable."
I always change this to "Will be installed on local hard drive."
It's not an issue, changing the system environment variables is a snap, but is there any upside to leaving this un-ticked?
If you only have one version of Python installed, it won't matter.
If you have multiple versions installed, then the first one that appears in your system Path will be executed when you use the "python" command. Additionally, it can make older versions inaccessible without extra work. For example, I had a system with Python 2.7 installed and I added 3.2 on top of that and checked the option to to add Python.exe to the path during installation. After doing that, entering both "python" and "python3" on the command line opened up Python 3.2, so I would need to enter the full path to the 2.7 interpreter when I needed to execute 2.x scripts.
One upside I can think of is if you run multiple python versions in windows. So, you have c:\python34 and c:\python27 but both are in the path, you'll get whichever comes first, leading you to a possibly unexpected result.
What line of text should I place at the top of a script.py to invoke the specific version of Python that I need to use?
I have two versions of Python on Windows XP, 2.6.5 and 2.7.2. They each have their own special modules and were installed by separate applications. My scripts are placed on the desktop so that I can double-click and run them conveniently.
The problem is that all my scripts invoke 2.6.5, which is fine for the scripts that use the modules installed with 2.6.5, but my scripts intended for 2.7.2 don't run. They invoke the Python 2.6.5 without the modules I need to import.
I've tried typing various headers without and without the #! to invoke 2.7.2 when I need to, but either my syntax is wrong or it just isn't possible to specify under Windows. Could anyone tell me the precise syntax of the line I need to add to my script. The python.exe for 2.7.2 is stored under C:\OSGeo4W\bin
Thanks for letting me know what line to place at the top of a script.py to invoke the exact version of Python I need to use.
If you have installed Python 3.3 on your system it added a new launcher for Python scripts which means you can use a shebang line:
#!python2.7
print "Hello"
or
#!python3.3
print("World")
will both work and run the appropriate python, or you can specify a full path to a Python interpreter or create an ini file that defines abbreviations for specific Python interpreters.
See PEP 397 for the different options available in Windows shebang lines.
If you don't want to install Python 3.3 then you can also install the launcher separately.
Instead of putting the script itself on the desktop, place a shortcut on the desktop. The process is described in a techrepublic.com article. Specify the appropriate interpreter as the program to run, and list one of your .py files as a parameter in the same field.
There is not "shebang" notation on Windows.
You'll need to change the file association for .py files to use your 2.7.2 installation ("Open With", "Use application as default").
I have always used a mac to write and run python scripts. However, I have a bunch of files on a PC that I need to run a script on. I have never really used a PC and don't know how to run the script.
If I go to the command program on the PC and type in python, nothing happens. How do I find where the python path is in order to get into the python prompt? Also, once I am in the prompt, is the importing of modules the same as in a Unix system?
Python isn't added to the system environment's PATH variable by default on windows. You have to either add the path to the directory containing the Python.exe file to the PATH variable, or call python explicitly.
This issue has been addressed in the Python documentation:
Python Documentation: # How to run a Python program under windows
Assuming Python is installed, it is usually placed in a folder prefixed with "Python" and the major/minor version. E.g. C:\Python26