Welcome to follow the official wechat account: FSA Full stack action 👋

Purpose: In a simple sentence, implement Python environment isolation

A, pyenv

1, install,

A:Git Clone

git clone git://github.com/pyenv/pyenv.git ~/.pyenv
Copy the code

bash

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/shims:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
exec $SHELL -l
Copy the code

zsh

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/shims:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
exec $SHELL -l
Copy the code

Method 2:Homebrew

The installation

brew install pyenv
Copy the code

Based on your environment, add the following content to the corresponding file:.bashrc /.zshrc

export PYENV_ROOT=/usr/local/var/pyenv
export PATH="$PYENV_ROOT/bin:$PATH"
export PATH="$PYENV_ROOT/shims:$PATH"
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
Copy the code

2, validation,

Verify the successful installation:

pyenv
Copy the code

3, use,

The basic use

The command describe
pyenv --version To viewpyenvThe version of the
pyenv versions Lists all that are currently installedpythonEnvironment, if the environment is currently in use, will be preceded by*
pyenv help See the help
pyenv init If the inputpyenvAfter usingtabIf you do not complete the command, you can use this command to initialize the command

Installation environment

The command describe
pyenv install -l Displays a list of available versions
Pyenv Install Version number Installs the specified versionpython
pyenv rehash Update the local database to install the specified versionpythonAfter using

Environmental applications

The command describe
Pyenv Global Version number To change the native version, restarting does not cause changes again
Pyenv Local Version number Will be created in the current directory.python-versionFile and record the SettingspythonEnvironment, which is automatically set to this directory each time you enter the directorypythonThe environment
Pyenv Shell version number To change the currentshellThe use ofpythonThis version is temporary and has a higher priorityglobal

Virtualenv plugin

1, install,

A:Git Clone

CD. Pyenv/plugins git clone https://github.com/pyenv/pyenv-virtualenv.git # virtualenv plug-in installationCopy the code

Method 2:Homebrew

brew install pyenv-virtualenv
Copy the code

2, configuration,

Regardless of the installation method, add the following content to the corresponding file:.bashrc /.zshrc based on your environment

eval "$(pyenv virtualenv-init -)"
Copy the code

3, use,

The basic use

The command describe
Pyenv virtualenv 3.8.3 env383 create3.8.3Version virtual environment
pyenv virtualenvs According to the environment
pyenv activate env383 Activation uses the specified virtual environment
pyenv deactivate Exit the current virtual environment
Rm - rf. Pyenv/versions / 3.8.3 Deleting a Version Environment
rm -rf .pyenv/versions/env383 Deleting a Virtual Environment

Automatically activate and exit the virtual environment

In directories where virtual environments are required (usually project directories) :

  1. To establish a.python-versionText file of
  2. Name the virtual environment (e.genv383) Write it inside

The virtual environment will be automatically activated/deactivated each time you enter/exit the directory.

Third, an error

1. An error is reported when activating the specified virtual environment

#The command
pyenv activate env383

#Error message
Failed to activate virtualenv.

Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
Copy the code

Don’t panic, restart your terminal, or execute the following command

exec $SHELL -l
Copy the code

2. System version

If you use Pyenv versions, the switch is successful, but python -v is still the system version.

Pyenv is not added to the $PATH environment variable.

export PYENV_ROOT=~/.pyenv
export PATH=$PYENV_ROOT/shims:$PATH
Copy the code

3, installation,pythonAn error

Python installation error, as shown in

Pyenv install 3.8.3Copy the code

Error content:

Python-build: use [email protected] from Homebrew python-build: Use readline from Homebrew Downloading python-38.3.tar.xz...-> https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tar.xzInstalling Python - 3.8.3... python-build: use readline from homebrew python-build: Use zlib from xcode SDK BUILD FAILED (OS X 12.0.1 using python-build 20180424) Inspect or clean up the working tree at / var/folders/cy / 8 zkx5hys2tsgb1d_x42_r8qc0000gn/T/python - build. 20220122174148.1531 the Results logged to / var/folders/cy / 8 zkx5hys2tsgb1d_x42_r8qc0000gn/T/python - build. 20220122174148.1531 the log Last 10 log lines: clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -i/usr/local/opt/readline/include -i/usr/local/var/pyenv/versions / 3.8.3 / include - I/usr/local/opt/sqlite/include -i/usr/local/opt/readline/include -i/usr/local/opt/readline/include -i/usr/local/var/pyenv/versions / 3.8.3 / include -I/usr/local/opt/sqlite/include -DPy_BUILD_CORE_BUILTIN -c ./Modules/_codecsmodule.c -o Modules/_codecsmodule.o clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -i/usr/local/opt/readline/include -i/usr/local/var/pyenv/versions / 3.8.3 / include - I/usr/local/opt/sqlite/include -i/usr/local/opt/readline/include -i/usr/local/opt/readline/include -i/usr/local/var/pyenv/versions / 3.8.3 / include -I/usr/local/opt/sqlite/include -DPy_BUILD_CORE_BUILTIN -c ./Modules/_weakref.c -o Modules/_weakref.o ./Modules/posixmodule.c:9221:15: error: implicit declaration of function 'sendfile' is invalid in C99 [-Werror,-Wimplicit-function-declaration] ret = sendfile(in, out, offset, &sbytes, &sf, flags); ^ clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -I/usr/local/opt/readline/include -i/usr/local/opt/readline/include -i/usr/local/var/pyenv/versions / 3.8.3 / include - I/usr/local/opt/sqlite/include -i/usr/local/opt/readline/include -i/usr/local/opt/readline/include -i/usr/local/var/pyenv/versions / 3.8.3 / include -I/usr/local/opt/sqlite/include -DPy_BUILD_CORE_BUILTIN -DPy_BUILD_CORE_BUILTIN -I./Include/internal -c ./Modules/_functoolsmodule.c -o Modules/_functoolsmodule.o 1 error generated. make: *** [Modules/posixmodule.o] Error 1 make: *** Waiting for unfinished jobs.... 1 warning generated.Copy the code

Use the following command to install, pay attention to modify the corresponding version number

CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/ lib-l $(brew --prefix bzip2)/lib" pyenv install --patch 3.8.3 < <(curl -sSL) https://github.com/python/cpython/commit/8ea6353.patch?full_index=1)Copy the code

Fourth, pay attention to

  • The installed version does not make any changes, we only operate virtual environments such asenv383
  • Multiple virtual environments can be versioned, such asenv383_1,env383_2
  • Export a dependency library for the current environment:pip freeze > requirements.txt
  • Import dependency libraries into the current environment:pip install -r requirements.txt