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
  • 207,228
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
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
  • 207,228
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
  • 277
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
  • 105,327
  • 107
  • 262
  • 331