Couple of questions about Python under Windows:
shebang:
I thought i read somewhere that the shebang is supposed to work correctly under Windows but it doesn't for me. So
#!/usr/bin/env python
#!/usr/bin/env python3
import sys
sys.version
No matter what shebang I use above I always get 2.7.6 on a system where both 2.7.6 and 3.3.3 are installed
2.Unfortunately Python 3.3 is not named python3.exe like on linux/mac. I want to have the ability to use both versions. I have my path variable set to 2.7.6 version first because thats what I'll probably use the most which means if I just type python thats the version. Is there any way to better manage this rather than having to type the full path to the executable or maybe dynamically changing the path variable?
Since there isn't usually a /usr/bin directory on Windows systems, even if shebang lines worked that wouldn't. You would instead need something like #!C:\Python33\bin\python.exe.
As far as I know, however, the shebang line is a Unix kernel feature that isn't available on Windows. I'd be happy to be wrong.
You would do well to look at using virtual environments - that allows you to set up the correct interpreter for each project, and keep the dependencies for different projects separate.
Related
I'm using the py Python launcher, and something weird is happening with imports. Certain modules can't be imported in scripts - despite being installed with pip and being importable in the interpreter. Here's the kicker, though: it works in scripts without the shebang line.
For example, this script works:
import requests
But this one doesn't:
#!/usr/bin/env python
import requests
Why is this? What difference is the shebang making here?
The py launcher actually handles shebang lines even on Windows. This means the Python version a script is run in may differ depending on the shebang line. For example, a shebang line of #!/usr/bin/env python2 will always run Python 2, and #!/usr/bin/env python3 will always run Python 3. Without a shebang line, py pretends the shebang is !python, which it handles using its built-in rules for selecting a Python version (see the Python Version Qualifiers section). By default, if you have it installed, this will be some version of Python 2.
Here's what's tripping you up, though. If the shebang is specifically !/usr/bin/env python, py instead follows the PATH environment variable to mimic the behavior of Linux's env (see the last paragraph), and only after that falls back on the normal behavior. This means scripts with the shebang might be running under a different Python version than py normally invokes, depending on what's in your PATH. This is what's happening here - #!/usr/bin/env python is finding another Python version on the path, and using that instead. That version might not have the modules you pip installed installed, and might therefore fail.
Since you're using py, you shouldn't have a python on your PATH, so remove any Python base directory entries from your PATH (e.g. C:\Python35), or make sure the first Python on the PATH is the default one used by py, and everything should work just fine.
Why py doesn't try to follow the path by default (or when the shebang is !python), however, is anyone's guess.
I'm planning to install Anaconda3 for Python 3.4. Since by default, Mac OS X uses Python2, will install Anaconda3 change the default Python version for the system? I don't want that to happen since Python3 can break backwards compatibility. If it does change the default Python version, how can I avoid that?
Apple has a built-in system for managing multiple versions of software, and switching between them. But you don't even need to worry about that, because Anaconda installations are self-contained: Everything lives under the top Anaconda installation directory (probably /Applications/anaconda). The only effect outside this directory is that during installation, Anaconda will offer to modify the PATH variable in your .bashrc. If you agree, it will add one line at the end of your .bashrc, something like this:
PATH="/Applications/anaconda/bin:$PATH"
As you can see, Anaconda puts itself first in the system path. This means that typing python at the shell prompt will launch python 3, which may not be what you want. I run Anaconda 3.4 like this and have had absolutely no problems with my system, but I did need to modify my own executable python2 scripts that launched python like this:
#!/usr/bin/env python
This is a nice way to find python wherever it is, but in this case it will find python 3-- oops! Changing the above to #!/usr/bin/python or to #!/usr/bin/env python2 ensures that they continue to work correctly. In my experience this was not necessary with any of the system's own scripts; everything is already set up to find the right python.
Alternative 1: You could decline the PATH modification, and use Anaconda via the launcher. In that case there is no change to the rest of your execution environment. The launcher will start a special bash prompt with the anaconda environment activated, but execution in normal shells is completely unaffected. If you will continue to program a lot in python 2, this may be for you.
Alternative 2: A minimal-impact alternative is to put the anaconda directory last in your path:
PATH="$PATH:/Applications/anaconda/bin"
This ensures that non-anaconda binaries take precedence over anaconda, so python will start good old /usr/bin/python (that is, python 2). You can start anaconda's variant by typing python3, idle3, etc. I did not have IPython before I installed anaconda, so typing ipython finds the anaconda version.
No it won't, you can have multiple python installs, once you don't remove your system python or manually change the default you will be fine.
I have 2 versions of python installed on my server.
Python 2.6 in /usr/bin/python
Python 2.7.8 in /usr/src
Python 2.7.8 install guide
How do I make python scripts use 2.7.8? instead of 2.6?
how do I set up an alias?
Thanks
Write:
#!<path to your python interpreter>
as the first line of the script your run. Then just execute it.
Other option:
<path to your python interpreter> your_script.py
There are multiple ways of doing this. First, I would suggest you check your $PATH environment variable to see if it holds the path to both of your python versions. If it doesn't, you can either add the path or simply make a symlink to it from /usr/bin/ and call it python2.7, for example.
Then you can use #Pavel's suggestion of specifying the path to the interpreter in the shebang of the file.
If you followed my previous instructions, your shebang should look something similar to the following.
#!/usr/bin/env python2.7
You could also use the second way #Pavel showed you, by simply calling the interpreter first in the terminal and pointing it to the script.
/usr/bin/python2.7 my_script.py
The best option you have, which I recommend, is using virtualenv as suggested before, but only if you want it for specific scripts. If you want it globally, I would suggest you read further.
Finally, if you want all your scripts to run under python 2.7 then I suggest you remove python 2.6 and install the 2.7 version from the package manager, the right way, and save yourself a lot of hassle.
I am using a module that can only be found in python 2.7, so when I run my script I have to specify python2.7 script instead of just script. I know there are bigger issues out there, but still I was wondering -
is it possible, when writing a python script, to set the interpreter to 2.7 by default?
Maybe by setting #! /usr/bin/env python for example?
Most unix environments will have the python2.7 executable, such that you can write:
#!/usr/bin/env python2.7
Obviously this doesn't help much on windows. You can also at least check the python version once you are started, although it won't help you run the later version if it is available:
import sys
print sys.version_info
...really do checks here...
Using the shebang is an appropriate way to specify this, yes. Find out where python2.7 is in your PATH using which python2.7. This is e.g. /usr/bin/python2.7. You then set the shebang:
#!/usr/bin/python2.7
Of course, this only works on those systems where Python 2.7 is available at this location :) This will also work on may systems: /usr/bin/env python2.7
Then make the script executable and fire it off using $ ./wonderful_python_script.py.
If you are using Windows, install Python 3.3, which features a new launcher that provides shebang support under Windows. This will give you a (finally!) sane and cross-platform way of explicitly specifying interpreter versions.
I installed python2.7 as an alternate version of python. I was attempting to utilize a newer version of mod_python and I needed 2.7. The default python (/bin/python) is 2.6. Unfortunately now, calling python from the command line calls /usr/local/bin/python2.7. I realize that I can set up a number of links pointing back to /bin/python--I just don't think this is a great idea. The OS (CentOS6) uses 2.6.2 by default, and I don't want the OS to use another version of python. I installed 2.7 from source, but forgot to specify 'make altinstall' rather than 'make install'. This is a semi-work related server, so I need to implement something that will permanently fix the problem. I realize .profile and .bashrc have paths for python, but these appear to be more for bash logins via ssh. I need to find a way to change the system's default python path back to 2.6.2. How would one go about doing this? Thank you for your help.
This is because /usr/local/bin comes before /bin in your $PATH.
What does which python say? I suspect it gives a symlink /usr/local/bin/python to /usr/local/bin/python2.7. Changing that symlink to /bin/python or removing it altogether should fix your problem.