Switch between Python versions as non root

前端 未结 3 1186
后悔当初
后悔当初 2021-01-06 12:38

I just installed Python 2.7 in our server, they had 2.4 installed before hand.

After I built the new version, my path is still pointing to the old version of python,

相关标签:
3条回答
  • 2021-01-06 13:07

    This answer assumes your environment is Linux, Unix or something similar.

    If your problem is the PATH environment variable not pointing at the correct binary, then modify that PATH: make it include a given directory, e.g. ~/bin, by executing PATH=${HOME}/bin:${PATH} from one of your profile files, e.g. ~/.profile. Make sure to create that directory, and have it contain a symlink named python pointing to the correct version of Python.

    This won't help for scripts which have a hardcoded path, e.g. start with #!/usr/bin/python. When you call these directly from the command line, you can simply call the interpreter (i.e. python) instead, passing the name of the script as an argument: python /path/to/script.py. You can even use the python version you want in its place, i.e. python2.7 /path/to/script.py.

    You might also turn this whole sequence into a script in your ~/bin directory, e.g. have ~/bin/foo contain the following content:

    #!/bin/sh
    exec ${HOME}/bin/python2.7 /usr/bin/foo
    

    Don't forget to chmod -x ~/bin/foo the file. You get an executable shell script which on your PATH comes before the default version installed on your system. The script will then call that default version, using a very specific interpreter. So now you can simply type the short name, and have the official script executed by your desired python version.

    It might happen that some other script attempt to execute a given python script using its absolute path name. In that case, no modification of the PATH will help. You'll have to modify the first line of the script in question to read e.g. to #!/usr/bin/env python. If you cannot control those scripts, you are in trouble, and will need more advanced hacks to tweak the system into doing something it usually wouldn't do. LD_PRELOAD comes to mind.

    0 讨论(0)
  • 2021-01-06 13:12

    Because you are on the server and don't have root permissions, the best choice for you is to use virtualenv

    Build Python 2.7, for example, as following:

    $ ./configure --prefix=~/mydir
    $ make
    $ make install
    

    Download virtualen.py file and run:

    $ ~/mydir/bin/python virtualenv.py my_environment
    

    This will create an isolated Python 2.7 environment for you inside my_environment directory.

    To activate it run source my_environment/bin/activate and that's it. Now python executable will be your Python 2.7. Additionally you will have pip installed and thus can easily install any additional libraries into your environment.

    0 讨论(0)
  • 2021-01-06 13:13

    First, you should check your ~/.profile and your shells config files (e.g. ~/.bashrc for bash) for any export $PATH= (or PYTHONPATH, depending on what you're referring to exactly) directives containing the offending path. If there are some, change them and relogin. That should fix your problem.

    If it doesn't, talk to your admin. If that doesn't help either, you can do the following. In your shell, run (may substitute PATH with PYTHONPATH, depending on what you really need):

    echo $PATH
    

    copy the output and modify it according to your needs. Then open another shell and run (remember the substitution):

    export PATH="whatever you copied before"
    

    check that everything is fine (i.e. that you can still call all applications you need and that your path is adapted accordingly). If that's the case, add the command to your ~/.profile.

    0 讨论(0)
提交回复
热议问题