pyenv implements python multi-version coexistence

install pyenv

Execute the following command in the terminal to install pyenv and several plugins:

$ curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
$ echo 'export PYENV_ROOT="$HOME/.pyenv"'>> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"'>> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
$ exec $SHELL -l

Install Python

View Installable Versions

$ pyenv install --list

This command will list the Python versions that can be installed with pyenv, just to name a few:

2.7.8   # Python 2 最新版本
3.4.1   # Python 3 最新版本
anaconda-4.0.0  # 支持 Python 2.6 和 2.7
anaconda3-4.0.0 # 支持 Python 3.3 和 3.4

Among them, those with only the version number in the form of x.x.xthis are the official versions of Python, and others in the form of xxxxx-x.x.x
this with both a name and a version are "derivatives" or releases.

Install Python dependencies

When installing Python, you need to install other packages that it depends on first. Some known libraries that need to be pre-installed are as follows.

Under CentOS/RHEL/Fedora:

sudo yum install readline readline-devel readline-static
sudo yum install openssl openssl-devel openssl-static
sudo yum install sqlite-devel
sudo yum install bzip2-devel bzip2-libs

Under Ubuntu:

sudo apt-get update
sudo apt-get install make build-essential libssl-dev zlib1g-dev
sudo apt-get install libbz2-dev libreadline-dev libsqlite3-dev wget curl
sudo apt-get install llvm libncurses5-dev libncursesw5-dev

Install the specified version

Install python 3.5.2 with the following command:

$ pyenv install 3.5.2 -v

This command will download the source code of python from github, extract it to the /tmpdirectory , and then
/tmpexecute the compilation in .
If the dependent package is not installed, a compilation error will occur, and you need to re-execute the command after installing the dependent package.

If the network is not very good, downloading with pyenv will be slower. You can execute this command first, then go to the ~/.pyenv/cache
directory to check the file name of the file to be downloaded, and then download it from the official website and put the file in the ~/.pyenv/cache
directory. pyenv will check the integrity of the file, and if it is correct, it will not download it again.

For the scientific research environment, it is more recommended to install the Anaconda distribution specially prepared for scientific computing,
pyenv install anaconda-7.0.0install the Python 2.x version, and
pyenv install anaconda3-5.0.0install the Python 3.x version;

update database

After the installation is complete, the database needs to be updated:

$ pyenv rehash

View currently installed python version

$ pyenv versions
* system (set by /home/seisman/.pyenv/version)
3.5.2

The asterisk in it indicates that the system's own python is currently being used.

Set the global python version

$ pyenv global 3.5.2
$ pyenv versions
system
* 3.5.2 (set by /home/seisman/.pyenv/version)

The current global python version has become 3.5.2. It is also possible to temporarily change the python version using pyenv localor .pyenv shell

confirm python version

$ python
Python 3.5.2 (default, Sep 10 2014, 17:10:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

use python

  • Enter pythonto use the new version of python;
  • The script that comes with /usr/bin/pythonthe directly call the old version of python in the way, so it will not affect the system script;
  • When using to pipinstall third-party modules, it will be installed ~/.pyenv/versions/3.4.1under , and will not conflict with system modules.
  • pipAfter installing the module using , it may be necessary to execute pyenv rehashupdate database;

Using USTC mirroring

If the speed of using pip to install modules is relatively slow, you can consider using the mirror provided by LUG of University of Science and Technology of China, which can greatly improve the speed of pip installation of modules.

Edit the ~/.pip/pip.conffile (create it if it doesn't already exist) and change index-urlthe line starting with to the following line:

[global]
index-url = https://pypi.mirrors.ustc.edu.cn/simple

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326425175&siteId=291194637