1

this might be a very simple question but I need your help. I work in a network and I cannot install the programs I want. Anyway, I need to use another version of python, which is installed in the directory /new_version/. Now, when I type "python" in a shell (I use bash) the command point to the version of python installed in the machine I'm working with. I'd love that when I type "python" this command point to the /new_version/ which I've installed. It would be also better if I can call this "new version" with another command, i.e. python2.

I tried changing the PYTHONPATH in the .bashrc but it didn't work.

0

3 Answers 3

2
alias newpython="/path/to/your/new_version/python"

Add this to your .bashrc, you can then start the new python with newpython and the standard one with python.

Sign up to request clarification or add additional context in comments.

1 Comment

Nope, he didn't. . It would be also better if I can call this "new version" with another command, i.e. python2.
2

Add the line

export PATH=/new_version/:$PATH

to your ~/.bashrc (or ~/.bash_profile) file. Then, whenever you run python, it will find the new version first in your PATH. Note this is PATH, not PYTHONPATH. See the comment by @Aaron.

Edit: Only do it this way if you want python to point to the new version. Use an alias as @cularis suggested if you want to call it something different, or make a symlink with:

ln -s /new_version/python /path/to/a/dir/you/add/to/your/path/newpython

3 Comments

I also tried with this but it didn't work. I guess because it looks first at the "python" installed on my machine...
Yes, but if you do this properly, the new version will be the first python it finds. Make sure you use the full path for the new version, and that it's first in your path.
@Matteo: You have to set PATH, not PYTHONPATH. The latter just sets the search path for Python modules. The former changes the rules where to find the Python executable (which will know where to find its modules). If in doubt, set both.
1

Install virtualenv. With this you can easily set up different Python versions like that:

virtualenv -p /new_version/bin/python

Also, virtualenv enables you to easily install other Python packages via pip install.

And finally, there's a package called tox which can automate testing with different Python versions...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.