Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new default packages to virtualenv?

When I create a virtualenv, it installs setuptools and pip. Is it possible to add new packages to this list?

Example use cases:

  • Following this solution to use ipython in virtualenv (from this question) requires installing ipython in every virtualenv (unless I allow system-site-packages).
  • Or if I'm doing a only flask/pygame/framework development, I'd want it in every virtualenv.
like image 970
idbrii Avatar asked Aug 19 '12 07:08

idbrii


People also ask

What is the default version of virtualenv in Python?

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.

How do I install requirements in a virtualenv?

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

Should I create a virtual environment for all Python packages?

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.

How do I install a seed package using virtualenv?

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).


2 Answers

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.

like image 54
RandomGuy Avatar answered Sep 20 '22 08:09

RandomGuy


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.

like image 20
MSS Avatar answered Sep 19 '22 08:09

MSS