Multiple python versions linux

What is the proper way to manage multiple python versions?

I have a machine with Python 2.6 installed as the default Python. Then, I installed Python 2.7, and manually created /usr/bin/python as a symlink to the new installation. Then, I was running into problems with command-not-found. I’m trying to reinstall it:

sudo apt-get remove command-not-found 
/usr/bin/python does not match the python default version. It must be reset to point to python2.6 

5 Answers 5

Changing the default Python (or Perl, etc) on an OS is really bad idea. This interpreter is actually part of the OS and there may well be other OS components that are written specifically to work with that version of the interpreter.

For example on Redhat the yum tool that performs system software updates is a python application. You really don’t want to break this. Such applications may depend on specific, perhaps non standard, python modules being installed which the version you installed may not have. For example on Ubuntu I believe some of the built-in OS tools written in Python use an ORM called Storm that isn’t part of the Python standard library. Does your clean Python 2.7 install have the specific expected version of the Storm module installed? Does it have any version of Storm? No? Then you’ve just broken a chunk of your OS.

The right way to do this is install your preferred version of python and set up your user account to use it by setting up your .bash_profile, path and such. You might also want to look into the virtualenv module for Python.

Источник

Pyenv – Install Multiple Python Versions for Specific Project

Managing multiple versions of Python on a Linux system is not an easy task, especially for beginners. Sometimes it even gets worse when you want to develop and run multiple projects with different Python versions on the same server. However, this shouldn’t be the case if you employ pyenv.

What is Pyenv?

Pyenv is a simple, powerful and cross-platform tool for managing multiple Python versions on Linux systems, that used for.

  • Switching the global Python version on a per-user basis.
  • setting the local Python version on per-project basis.
  • Managing of virtual environments created by anaconda or virtualenv.
  • Overriding the Python version with an environment variable.
  • Searching commands from multiple versions of Python and more.
Читайте также:  Kali linux установка инструментов

How Does pyenv Work?

Usually, a single default version of Python is used to run all your applications, unless you explicitly specify the version you want to use within the application. But pyenv implements a simple concept of using shims (lightweight executables) to pass your command to the correct Python version you want to use, when you have multiple versions installed.

These shims are inserted by pyenv in a directories in front of your PATH. So when you run a Python command, it is intercepted by the appropriate shim and passed to pyenv, which then establishes the Python version that has been specified by your application, and passes your commands along to the rightful Python installation. This is an overview of how pyenv operates.

In this article, we will show how to install the latest version of pyenv in Linux. We will also demonstrate the first three uses case listed above.

How to Install Pyenv in Linux

1. First install all the required packages for installing different Python versions from sources using following command on your respective Linux distribution.

------------ On Debian/Ubuntu/Linux Mint ------------ $ sudo apt install curl git-core gcc make zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libssl-dev ------------ On CentOS/RHEL ------------ # yum -y install epel-release # yum install git gcc zlib-devel bzip2-devel readline-devel sqlite-devel openssl-devel ------------ On Fedora 22+ ------------ # yum install git gcc zlib-devel bzip2-devel readline-devel sqlite-devel openssl-devel

2. Next, grab the the latest pyenv source tree from its Github repository and install it in $HOME/.pyenv path using following command.

$ git clone https://github.com/pyenv/pyenv.git $HOME/.pyenv

3. Now you need to set the environment variable PYENV_ROOT to point to the path where you installed pyenv and export it. Then add $PYENV_ROOT/bin to your PATH to run pyenv command-line utility like any other system commands.

You also need to enable shims as well as autocompletion by adding the pyenv init to your shell. Do all these things in your $HOME/.bashrc bash startup file, as shown.

Читайте также:  Cryptopro linux amd64 tgz

Copy and paste the following lines at the end of this file.

## pyenv configs export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)" fi

4. Once you have made the above changes, you can either source $HOME/.bashrc file or restart the shell as shown.

$ source $HOME/.bashrc OR $ exec "$SHELL"

How to Install Multiple Python Versions in Linux

5. At this point, you should be ready to start using pyenv. Before you install any Python version, you can view all available versions with this command.

List Multiple Python Versions

6. You can now install multiple Python version via pyenv, for example.

$ pyenv install 3.6.4 $ pyenv install 3.6.5

Install Multiple Python Versions

7. To list all Python versions available to pyenv, run the following command. This will only show versions installed via pyenv itself.

List Installed Python Versions

8. You can check the global Python version with the following command, by this time, the default version should be the one set by the system, not pyenv.

You can set the global python version using the pyenv command.

$ pyenv global 3.6.5 $ pyenv global

Set Global Python Version

9. You can now set the local Python version on per-project basis, for instance, if you have a project located in $HOME/python_projects/test, you can set the Python version of it using following command.

$ cd python_projects/test $ pyenv local 3.6.5 $ pyenv version #view local python version for a specific project OR $ pyenv versions

Set Python Version for Project

10. Pyenv manages virtual environments via the pyenv-virtualenv plugin which automates management of virtualenvs and conda environments for Python on Linux and other UNIX-like systems.

You can start by installing this plugin using following commands.

$ git clone https://github.com/yyuu/pyenv-virtualenv.git $HOME/.pyenv/plugins/pyenv-virtualenv $ source $HOME/.bashrc

11. Now we will create a test virtual environment called venv_project1 under a project called project1 as follows.

$ cd python_projects $ mkdir project1 $ cd project1 $ pyenv virtualenv 3.6.5 venv_project1

Create a Virtual Environment

12. Now when you list all Python versions, your virtual environments as well as their local python versions should be listed also, as shown in the screenshot.

List Python Versions

13. To activate a virtualenv, for example venv_project1, type following command.

$ pyenv activate venv_project1

Note: You may get the message below while using the latest version of pyenv-virtualenv plugin for the first time.

pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.

Add the line export PYENV_VIRTUALENV_DISABLE_PROMPT=1 in your $HOME/.bashrc file, where you added other pyenv configs, and source the file to simulate the behavior being emphasized.

Читайте также:  Сменить пароль ftp linux

14. To deactivate the activated virtualenv, run this command.

For more information, you can list all pyenv commands using following command.

For more information, go to the pyenv Github repository: https://github.com/pyenv/pyenv

Using pyenv is really that simple. In this guide, we showed how to install it, as well as demonstrated some of its use cases for managing multiple python versions on a Linux system. Use the feedback form below to ask any questions or share your thoughts about this tool.

Источник

Multiple Python versions on the same machine?

Is there official documentation on the Python website somewhere, on how to install and run multiple versions of Python on the same machine on Linux? I can find gazillions of blog posts and answers, but I want to know if there is a «standard» official way of doing this? Or is this all dependent on OS?

11 Answers 11

I think it is totally independent. Just install them, then you have the commands e.g. /usr/bin/python2.5 and /usr/bin/python2.6 . Link /usr/bin/python to the one you want to use as default.

All the libraries are in separate folders (named after the version) anyway.

If you want to compile the versions manually, this is from the readme file of the Python source code:

Installing multiple versions

On Unix and Mac systems if you intend to install multiple versions of Python using the same installation prefix (—prefix argument to the configure script) you must take care that your primary python executable is not overwritten by the installation of a different version. All files and directories installed using «make altinstall» contain the major and minor version and can thus live side-by-side. «make install» also creates $/bin/python3 which refers to $/bin/pythonX.Y. If you intend to install multiple versions using the same prefix you must decide which version (if any) is your «primary» version. Install that version using «make install». Install all other versions using «make altinstall».

For example, if you want to install Python 2.5, 2.6 and 3.0 with 2.6 being the primary version, you would execute «make install» in your 2.6 build directory and «make altinstall» in the others.

Источник

Оцените статью
Adblock
detector