Further Configuration of pip and Virtualenv

https://d33wubrfki0l68.cloudfront.net/740dd32b37daec5f4d9d475ed3598ebeec4d39b1/a911a/_images/34018732105_f0e6758859_k_d.jpg

Requiring an active virtual environment for pip

By now it should be clear that using virtual environments is a great way tokeep your development environment clean and keeping different projects’requirements separate.

When you start working on many different projects, it can be hard to remember toactivate the related virtual environment when you come back to a specificproject. As a result of this, it is very easy to install packages globallywhile thinking that you are actually installing the package for the virtualenvironment of the project. Over time this can result in a messy global packagelist.

In order to make sure that you install packages to your active virtualenvironment when you use pip install, consider adding the followingline to your ~/.bashrc file:

  1. export PIP_REQUIRE_VIRTUALENV=true

After saving this change and sourcing the ~/.bashrc file withsource ~/.bashrc, pip will no longer let you install packages if you are notin a virtual environment. If you try to use pip install outside of avirtual environment pip will gently remind you that an activated virtualenvironment is needed to install packages.

  1. $ pip install requests
  2. Could not find an activated virtualenv (required).

You can also do this configuration by editing your pip.conf orpip.ini file. pip.conf is used by Unix and Mac OS X operatingsystems and it can be found at:

  1. $HOME/.pip/pip.conf

Similarly, the pip.ini file is used by Windows operating systems and itcan be found at:

  1. %HOME%\pip\pip.ini

If you don’t have a pip.conf or pip.ini file at these locations,you can create a new file with the correct name for your operating system.

If you already have a configuration file, just add the following line under the[global] settings to require an active virtual environment:

  1. require-virtualenv = true

If you did not have a configuration file, you will need to create a new one andadd the following lines to this new file:

  1. [global]
  2. require-virtualenv = true

You will of course need to install some packages globally (usually ones thatyou use across different projects consistently) and this can be accomplished byadding the following to your ~/.bashrc file:

  1. gpip() {
  2. PIP_REQUIRE_VIRTUALENV="" pip "$@"
  3. }

After saving the changes and sourcing your ~/.bashrc file you can nowinstall packages globally by running gpip install. You can change the nameof the function to anything you like, just keep in mind that you will have touse that name when trying to install packages globally with pip.

Caching packages for future use

Every developer has preferred libraries and when you are working on a lot ofdifferent projects, you are bound to have some overlap between the librariesthat you use. For example, you may be using the requests library in a lotof different projects.

It is surely unnecessary to re-download the same packages/libraries each timeyou start working on a new project (and in a new virtual environment as aresult). Fortunately, starting with version 6.0, pip provides an on-by-defaultcaching mechanism that doesn’tneed any configuration.

When using older versions, you can configure pip in such a way that it tries toreuse already installed packages, too.

On Unix systems, you can add the following line to your .bashrc or.bash_profile file.

  1. export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache

You can set the path to anywhere you like (as long as you have writeaccess). After adding this line, source your .bashrc(or .bash_profile) file and you will be all set.

Another way of doing the same configuration is via the pip.conf orpip.ini files, depending on your system. If you are on Windows, you canadd the following line to your pip.ini file under [global] settings:

  1. download-cache = %HOME%\pip\cache

Similarly, on Unix systems you should simply add the following line to yourpip.conf file under [global] settings:

  1. download-cache = $HOME/.pip/cache

Even though you can use any path you like to store your cache, it is recommendedthat you create a new folder in the folder where your pip.conf orpip.ini file lives. If you don’t trust yourself with all of this pathvoodoo, just use the values provided here and you will be fine.

原文: https://docs.python-guide.org/dev/pip-virtualenv/