Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Introduction to Python version management tools and virtual environments


Jun 01, 2021 Article blog


Table of contents


Here's a brief introduction to Python version management tools -- pyenv and three virtual environments: virtualenv anconda pipenv

Version management tool -- pyenv

This is a python version management package, you can download the source code directly through git, the installation method has https://github.com/pyenv/pyenv git address, download down, and then step by step to do it.

Take a brief look at a few path meanings

  1. ~/.pyenv/shims/

The python command, which is stored here, was executed when we python in the terminal, and we can print the PATH and see that the command for this path is at the front. When we enter python pyenv goes to find the python command that we set up to actually execute.

  1. ~/.pyenv/versions/

This directory contains the python version we installed. ( Note: Don't worry if we install it very slowly.) W e can print out the address of the terminal, put it in the browser, and then download it down, and move the downloaded file to ~/.pyenv/cache/ can be. Just started building this cache directory yourself)

(Recommended tutorial: python tutorial)

The python version is set with two commands

  1. pyenv local name: Use this python version in the current directory
  2. pyenv global name: Set the global python version to this version

The name we can view through pyenv versions there will be a system name, this is the original python version of your machine, in general we use local to set python for a directory, global or with our system. With local we can find a .python_version file in the current .pyenv should simply read this file and know what version of python you want to use in the current directory.

Having used node may reveal that this is actually about the same nature as node nvm

Virtual environment -- virtualenv

The virtual environment I first used was virtualenv which is also used in a whole host of applications online. To put it simply, because I rarely use it now.

安装:pip install virtualenv
创建:virtualenv env名称
进入虚拟环境:source env名称/bin/activate
退出虚拟环境:deactivate

Enter the environment and you'll be able to use pip install inside. The package installed is in the current environment.

Virtual environment -- anconda

Later I saw that there was an anconda package manager that could also create virtual environments. There are also many installation tutorials for this.

安装:官网有教程,下载下来运行就可以了
创建:conda create -n env名称 python=2.7
进入:conda activate env名称
退出:conda deactivate

Into the environment, you can also install the package inside, which uses anconda command: conda install package. Some packages may not be found in this, and you can also install them with pip install

Note: It's a bit of a pit to install with pip if you already have this package installed on your local machine, then pip install pip install can't be installed, and if you're installing a package that's not the same as your native version, he'll uninstall the package for the machine and then reinstall a new package in your conda environment. S o you don't have this bag on this machine. F or a new machine, it may be better, after all, after all, running projects in a virtual environment, but for some people who have a native environment to run the project, it is not so friendly, it is possible that you use anconda inexplicably the native is missing the package. Of course you can install it again.

Virtual environment -- pipenv

Later, I recently found a pipenv which feels better.

安装:pip install pipenv
创建:pipenv install --python=2.7
进入:进入目录,pipenv shell
退出:deactivate

Create a virtual environment that is stored in the default directory, my default directory is under ~.local/share/ and then create a Pipfile file in the current directory. I t records the package you installed. T he installation package uses pipenv install the package is recorded in Pipfile and if you already have Pipfile in the current directory, you pipenv install he creates a virtual environment associated with the current directory, and then installs the package in Pipfile I nside, you can set up the source of the download package. t o increase download speed. W hen installed, a Pipfile.lock file is generated. I t records some information about the real downloaded package, and when the project migrates, the environment in which these directories are run together, no matter where they are, is the same. That's one of my favorite things, node bit like node's package.json file feature.

(Recommended micro-course: python3 basic micro-course)

The above is about Python version management tools and virtual environments, I hope to help you.