4

I am using a server that I do not have admin access on. There are several versions of pythons installed. Say one is in /some/home/directory/Python2.6/ and the other in /some/home/directory/Python2.7/.

Is there a simple way of changing the python version in terminal temporarily, without changing the default version of python, and without requiring root access (all the answers I have found so far does/requires one of those conditions)?

muru
  • 197,895
  • 55
  • 485
  • 740
hsnee
  • 145

4 Answers4

3

To change the python version for your terminal session you can create an alias in your .bashrc file then re-login.

alias python='/usr/bin/python3.4'

The link to the following article provides detailed instructions to change to an alternate Python version per user session.

nuwandame
  • 126
  • Any idea where such a file would be for each user? (would it be in a home directory or my own working directory?) or alternatively would running this within terminal change the python version in the current session? – hsnee Nov 17 '15 at 15:59
  • 1
    Normally you can find the .bashrc in your user account home directory i.e. /home/username/.bashrc. You can edit the file using vi by simply typing vi ~/.bashrc – nuwandame Nov 18 '15 at 04:43
2

My recommendation would be to use an alias to "override" the python command.

An alias can be created with the same name as the core name of a command (i.e., a command without any options or arguments). In such case, it is the alias that is called (i.e., activated) first when the name is used, rather than the command with the same name. For example, an alias named ls could be created for the command ls -al as follows:

alias ls="ls -al" 

ls is a commonly used command that by default lists the names of the files and directories within the current directory (i.e., the directory in which the user is currently working). The -a option instructs ls to also show any hidden files and directories, and the -l option tells it to provide detailed information about each file and subdirectory.

Such an alias can be disabled temporarily and the core command called by preceding it directly (i.e., with no spaces in between) with a backslash, i.e.,

\ls 

Taken from linfo.org

muru
  • 197,895
  • 55
  • 485
  • 740
mascoj
  • 121
1

As commented to the question, Virtual Env manages all pythons you have - Just use it

# install pip and venv as user
python3 -m pip install --user --upgrade pip
python3 -m pip install --user virtualenv

#create the virtual env files and folders in the current dir python3 -m venv env

activate the virtual env by sourcing the bin file

source env/bin/activate

#confirm it which python

Timo
  • 237
1

In your program if you mention #!/usr/bin/python2.6 as the first line then your program will consider python 2.6 as its runtime environment. And similarly if you mention python2.7 it will consider from python2.7.

If you want to access python from terminal then you can give python2.7 at terminal and you will get into it.

Raja G
  • 102,391
  • 106
  • 255
  • 328
  • 1
    NB: #!/usr/bin/env python2.7 may work better than directly inputting the path to Python, and makes your code more portable. –  Nov 17 '15 at 18:40