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.
Related
I bought my mac about a year ago and somehow changed my python symlink so that when I run python some_file.py, python 3.4 is used to run the file instead of python 2.7. I now need to change it back, but I can't figure out what I did to change it in the first place! When I run:
import os
os.path.realpath("/usr/local/bin/python")
in the python terminal, the output is:
'/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7'
Does this not mean that my python symlink is pointing to my python 2.7 version, and not my 3.4 version? If not, how do I find out which file is run when I use the python symlink?
You probably installed that specific Python version using the official Python installer for OS X; see the Using Python on a Macintosh documentation. The installer creates the /usr/local/bin symlink for you.
If you also, at some point, had 3.4 installed then that installation is still there too. Check for a /usr/local/bin/python3 command; it'll link to the existing Python 3 binary. Use that instead to run Python 3 code.
If you do have a /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 command, you could re-create the /usr/local/bin/python symlink to point there instead, but I'd personally only use the python3 name for Python 3 scripts.
Last, you could also have used the homebrew tool to install Python; it can manage symlinks for you. However, homebrew installs Python binaries into the /usr/local/Cellar tree structure instead.
I am doing maintenance for a python code. Python is installed in /usr/bin, the code installed in /aaa, a python 2.5 installed under /aaa/python2.5. Each time I run Python, it use /usr/bin one. How to make it run /aaa/python2.5?
Also when I run Python -v; import bbb; bbb.__file__; it will automatically show it use bbb module under /usr/ccc/(don't know why), instead of use bbb module under /aaa/python2.5/lib
How to let it run python2.5 and use `/aaa/python2.5/lib' module? The reason I asking this is if we maintain a code, but other people is still using it, we need to install the code under a new directory and modify it, run it and debug it.
Change the shebang-line (if there is such a line)
Nomally
#!/usr/bin/env python
is used to start the python interpreter first found in the path, this is how virtualenv works, i.e. it changes the path so that the chosen interpreter is placed first and the line above simply works.
In your example, change i to
#!/aaa/python2.5
Note that the executable-flag has to be set for this to work.
Example:
$ chmod u+x script.py
$ ./script.py
See PYTHONPATH for a description of how python searches for modules.
Do /aaa/python2.5 python_code.py. If you use Python 2.5 more often, consider changing the $PATH variable to make Python 2.5 the default.
Add this to your .bash_profile file (create if non exist, it's a hidden file):
python25(){
/aaa/python2.5 "$1"
}
Now you can use Python (latest) and Python 2.5:
$ python my_file.py
$ python25 my_file.py
Good luck!
I recently installed epd python distribution in ubuntu. This got installed in the folder /home/jai/Downloads/epd_free-7.3-2-rh5-x86_64
Can you tell me how to make this python as my default python?
I get errors while running a test program (it seems my default python is different and it doesn't have numpy library, other libraries that come along epd python distribution.)
My test program is here: http://www.southampton.ac.uk/~fangohr/computing/download/python/tests/testall.py
The "default" python depends on how you're invoking it.
On Ubuntu, python is normally installed as /usr/bin/python (not /bin/python) -- which may be a symbolic link.
If you invoke the python command, e.g.:
$ python myscript.py
it will use whichever python executable is in a directory that appears first in your $PATH. You can modify your $PATH, either for your current shell:
export PATH="/some/dir:$PATH"
or for all future shells by updating your $HOME/.bashrc, $HOME/.bash_profile, or whatever. /usr/local/bin is one common place to put system-specific executables, or $HOME/bin for user-specific executables.
If you want to execute the script itself, you'll need a shebang as the first line of the script:
$ head -1 myscript.py
#!/usr/bin/python
$ ./myscript.py
...
You can edit the shebang to refer to whatever Python executable you want to use.
You can replace /usr/bin/python with your preferred Python executable, but that might cause unwanted side effects; existing Python scripts that assume /usr/bin/python is the default might break.
Another option is to change the shebang to:
#!/usr/bin/env python
which lets you execute the script directly while still using whichever python is first in your $PATH. This may or may not be a good idea; see my answer to this question for further discussion.
Default python is the one found in /usr/bin directory with the name python. Making a symbolic link of :
ln -s /home/jai/Downloads/epd_free-7.3-2-rh5-x86_64 /usr/bin/python
Assuming that is the name of the python executable, not the installer. After you installed, use the path where you installed it. f.e /home/iai/myNewPythonInstallation
might do the trick.
Most likely default 2.7 python is occupying that name, so you need to remove that, or use another name like epdPython. Then running python scripts would happen with:
epdPython myscript.py
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.
What's the best way to set up symbolic links to current installs, e.g python -> python2.6?
I've just installed python2.6 through Macports at /opt/local/bin/python2.6, I'd now like to set up a symbolic link called python here /usr/local/bin/. I then want to be able to add this line at the beginning of my pythons scripts so it knows where to look: #!/usr/local/bin/python. But what happens when I upgrade python to python2.7 for example, do I just need to remember to go to my symbolic link and change it? I guess I'll remember because it likely won't work anymore? Is there a better way to do this?
By default, MacPorts deliberately and carefully installs everything into a separate directory space: /opt/local. This ensures it does not conflict with anything installed as part of OS X or third-parties. To ensure that MacPorts-installed executables are found first, the recommended solution is to modify your shell PATH to put /opt/local/bin before /usr/bin.
MacPorts also provides a special port package, python_select, to manage which python version is pointed to by the command python in /opt/local/bin.
sudo port install python_select
sudo python_select
Then, to make your scripts use your current preferred python, the traditional solution is to use the env program in the shebang line of your scripts.
#!/usr/bin/env python
Symlink the version you use most.
When you need another version, run it by specifying the version number, e.g.:
$ python2.5 dev_appserver.py myapp
Not sure about OSX, here is what I do on Ubuntu 9.04:
>which python
#/usr/bin/python
Just replace that file with a sym link to the version of Python you actually want to use:
>sudo ln -s /usr/bin/python2.6/python /usr/bin/python