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.
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.
System A has both Python 2.7 and Python 3.4 installed.
System B has both Python 2.7 and Python 3.5 installed.
I have at top of Python script:
#!/usr/bin/env python3.5
The reason being that python3 compiler must be used. I want to move it between machines but this will fail now.
for me just
#!/usr/bin/env python3
works fine
Use Virtualenv to setup your python environment.
If your are set on using #!/usr/bin/env python3.5, you could create a symbolic link to the python3.4 version (called python3.5) and then reference that in your script. So both environments could use the directive #!/usr/bin/env python3.5. Of course, please add a comment somewhere that this is a symbolic link so people are aware of this environment situation.
In fact, I think the python in #!/usr/bin/env python solution is a symbolic link.
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.
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.
I'm starting on a Python 3 project, so I'd like to configure Emacs' run-python command to use the python3 interpreter by default.
I don't want to
change python so that it points to python3 instead of python2.7 (because that might cause various bad things)
pass "python3" as an argument to the run-python command each time I run it (because that sounds annoying)
I've tried setting python-which-shell to "python3" in my .emacs, but this doesn't seem to work.
On Emacs 24.3, add to your .emacs:
(setq python-shell-interpreter "python3")
If this doesn't work on an older Emacs with a different Python mode, try M-x find-function RET run-python RET to inspect the source for an equivalent variable that can be set.
You can use the shebang line to specify which version you want to run:
#!/usr/bin/env python
Will use whichever one the system considers "python"
#!/usr/bin/env python2.6
#!/usr/bin/env python2.7
#!/usr/bin/env python3.2
Will run the specified version if you have more than one installed.
A friend taught me this when our Ops guy set up a VM with 2.6 and 2.7 installed, and I required 2.7, while the CentOS package manager required 2.6. Saved me a lot of having to go back and make things 2.6-compatible.