简体   繁体   中英

How to make python program use different installation

>> which python
/usr/bin/python
>> python --version
Python 2.4.x

My problem is very simple, how do I run a python program on a different installation of python (eg /usr/bin/python26)?

I want to be able to run Scrapy with a different python installation than the default, but the question is meant for any program written in Python running on a Unix-like system.

EDIT: To clarify, I want to run an installed program which someone else wrote and is usually run like so:

scrapy crawl blaha

NOT like so:

python scrapy crawl blaha

Normally, under Unix systems, you have multiple Python executables, for example, on my system:

which python
/usr/bin/python
which python2
/usr/bin/python2

The name of the executable gives the version of the application. This is the standard for different versions of Python, as defined in PEP 394 . To hint at which version your script should use, the normal thing to do is provide a hashbang. For example, on my system, Python 3.x is the default Python, so if I write a 2.x script, I add:

#!/usr/bin/env python2

Then, when my script is run ( ./some_script.py ) it will use Python 2.7.3.

You should avoid using just #!/usr/bin/env python as it will do different things on different systems. If they are following the specifications, #!/usr/bin/env python2 should point to 2.x and #!/usr/bin/env python3 should point to 3.x - under Arch this works correctly (with just python pointing to 3.x (unusually) as well).

Update for your edit:

If you want to do that, the easiest route is to make an executable in your system path that simply runs the command python some_script ... .

This is done regularly, for example, eclipse on my system actually runs /usr/bin/eclipse - a bash script that contains:

#!/bin/bash
export ECLIPSE_HOME=/usr/share/eclipse
export GDK_NATIVE_WINDOWS=true
${ECLIPSE_HOME}/eclipse "$@"

So for your problem, just make a script somewhere in your path (let's say /usr/bin/scrapy ) which contains:

#!/bin/bash
python2 scrapy "$@"

I would note that in future, you should try to give as much information about your problem as possible up front, not changing it after getting answers.

How are you running your program? Does it have the execution bit set and you have the first line:

#!/usr/bin/python

Then, well, just do:

#!/usr/bin/python26

Or, you can just do:

>> python26 your-awesome-python-script.py

Regarding your edit: Since you can't / don't want to change the third party program (scrapy) you need to either:

  • relink /usr/bin/python (do an ls -l on it. You will see that it points to the python24 executable, do an ln -s /usr/bin/python /usr/bin/pythonXY or similar to relink this)
  • create an alias for scrapy in your .profile
  • create your own scrapy shell script that does the proper incantations (eg running the script with your preferred interpreter)

You could take a look to VirtualEnv which is designed to let you use different versions of python on your installation.

If you want to use an already installed python version, just use python2.6 instead of python if you want to use python2.6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM