jiansoung / issues-list

记录日常学习和开发遇到的问题。欢迎评论交流:)
https://github.com/jiansoung/issues-list/issues
MIT License
14 stars 0 forks source link

pyenv, pip and virtual environment #5

Open jiansoung opened 6 years ago

jiansoung commented 6 years ago

pyenv, pip and virtual environment

Notes on The Hitchhiker’s Guide to Python

pyenv and how does it work ?

pyenv is a tool to allow multiple versions of the Python interpreter to be installed at the same time.

pyenv works by filling a shims directory with fake versions of the Python interpreter (plus other tools like pip and 2to3). When the system looks for a program named python, it looks inside the shims directory first, and uses the fake version, which in turn passes the command on to pyenv. pyenv then works out which version of Python should be run based on environment variables, .python-version files, and the global default.

pip and virtual environment

In order to make sure that you install packages to your active virtual environment when you use pip install, consider adding the following lines to your ~/.bashrc (or ~/.zshrc) file:

export PIP_REQUIRE_VIRTUALENV=true

gpip() {
    PIP_REQUIRE_VIRTUALENV="" pip "$@"
} 

After saving this change and sourcing the ~/.bashrc file (or ~/.zshrc) with source ~/.bashrc (or source ~/.zshrc), pip will no longer let you install packages if you are not in a virtual environment. (But you can use gpip install packages globally. )

You can also do this configuration by editing your pip.conf or pip.ini file.

References

  1. The Hitchhiker’s Guide to Python!
  2. https://docs.python-guide.org/dev/pip-virtualenv/#requiring-an-active-virtual-environment-for-pip