When I create a virtualenv, it installs setuptools and pip. Is it possible to add new packages to this list?
Example use cases:
virtualenv being a python application has always at least one such available, the one virtualenv itself is using, and as such this is the default discovered element. This means that if you install virtualenv under python 3.8, virtualenv will by default create virtual environments that are also of version 3.8.
Typically the steps you always take are: virtualenv <my_env_name> to create a new environment source <my_env_name>/bin/activate to activate the new environment pip install -r requirements.txt to install the requirements in the current environment
The default behavior (without specifying either flag) is to create the virtual environment such that when you are using it, any Python packages installed outside the environment are not accessible. That's typically the right choice because it best isolates the virtual environment from your local computer environment.
To install a seed package via either pip or app-data method virtualenv needs to acquire a wheel of the target package. These wheels may be acquired from multiple locations as follows: virtualenv ships out of box with a set of embed wheels for all three seed packages (pip, setuptools, wheel).
I took a different approach from what is chosen as the correct answer.
I chose I directory, like ~/.virtualenv/deps
and installed packages in there by doing
pip install -U --target ~/.virtualenv/deps ...
Next in ~/.virtualenv/postmkvirtualenv
I put the following:
# find directory
SITEDIR=$(virtualenvwrapper_get_site_packages_dir)
PYVER=$(virtualenvwrapper_get_python_version)
# create new .pth file with our path depending of python version
if [[ $PYVER == 3* ]];
then
echo "$HOME/.virtualenvs/deps3/" > "$SITEDIR/extra.pth";
else
echo "$HOME/.virtualenvs/deps/" > "$SITEDIR/extra.pth";
fi
Post that basically says the same thing.
You can write a python script, say personalize_venv.py
that extends the EnvBuilder
class and override its post_setup()
method for installing any default packages that you need.
You can get the basic example from https://docs.python.org/3/library/venv.html#an-example-of-extending-envbuilder.
This doesn't need a hook. Directly run the script with command line argument dirs
pointing to your venv directory/directories. The hook is the post_setup()
method itself of EnvBuilder
class.
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