I really hate running something like make
and then be surprised by pip installing a load of packages because I forgot to activate my virtualenv.
Is there any way to force pip to prompt / warn me that I'm not in some virtualenv?
There are also workflow tools that simplify this process, such as Pipenv and Poetry. They create virtual environments for you without perception and then install dependencies into them. They are used by a wide range of users.
Traditionally virtualenv has been the library used to create virtual environments for python. However , starting python 3.3 , module venv has been added to python standard library and can be used as a drop-in replacement for virtualenv. If older version of python is being used, then virtualenv is the way to go.
If you're in an active virtual environment, then the command installs pip into that environment. Otherwise, it installs pip globally on your system.
Activating a virtual environmentAs long as your virtual environment is activated pip will install packages into that specific environment and you'll be able to import and use packages in your Python application.
Taken from http://docs.python-guide.org/en/latest/dev/pip-virtualenv/
You need to set a environmental variable PIP_REQUIRE_VIRTUALENV
Best practice would be to place it in your autostart file (.bash_profile
or similar)
export PIP_REQUIRE_VIRTUALENV=true
To install the package globally, you can then run PIP_REQUIRE_VIRTUALENV="" pip ...
or create a command gpip
, also in the autostart file:
gpip() {
PIP_REQUIRE_VIRTUALENV="" pip "$@"
}
Make a note that pip version 20.2.3 from Python 3.6 requires PIP_REQUIRE_VIRTUALENV=false
rather than PIP_REQUIRE_VIRTUALENV=""
to install global dependencies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With