c12
c12

Reputation: 9827

Execute Python Script From Non-System Installed Python

I currently have python 2.7.1 installed on my Mac and there are some features of 2.7.3 that I would like to use with my current Python development. I've downloaded the and extracted the tar ball into my /Users/mark/Python-2.7.3 but when I run my Python script how do I tell it to use the new 2.7.3 version instead of the system installed 2.7.1?

/Users/mark/Python-2.7.3
MacBook-Pro:Python-2.7.3 mark$ ls
Demo        Lib     Objects     README      install-sh
Doc         Mac     PC          RISCOS      pyconfig.h.in
Grammar     Makefile.pre.in PCbuild     Tools       setup.py
Include     Misc        Parser      configure
LICENSE     Modules     Python      configure.in

Upvotes: 1

Views: 120

Answers (2)

iabdalkader
iabdalkader

Reputation: 17312

you could either use the interpreter you want to run the script directly:

/Users/mark/Python-2.7.3/python2.7.3 script.py

or set the interpreter in the shebang line:

#!/Users/mark/Python-2.7.3/python2.7.3

third option, prepend the path to the interpreter to the PATH variable:

export PATH=/Users/mark/Python-2.7.3/:$PATH

Upvotes: 1

cleg
cleg

Reputation: 5022

In such cases — virtualenv is highly recommended. Install virtualenv, and create new venv, using -p key, pointing to another python version.

virtualenv -p /Users/mark/Python-2.7.3/python2.7.3

Then simply run

source bin/activate

And in this shell session, all calls to python will be done to isolated version of python with given version.

Virtualenv is a little bit harder to laern and use, but I'd suggest to use it always for python development. Keeping all projects and their dependencies isolated — helps very much. And for easy use of virtualenv, there is an excellent virtualenwrapper.

Upvotes: 1

Related Questions