venv arguments
So far, we have just created a plain and regular venv, but there are a few, really useful flags for customizing your venv specifically to your needs.
First, let's look at the venv help:
Parameter | Description |
---|---|
--system-site-packages | It gives the virtual environment access to the system-site-packages directory |
--symlinks | Try to use symlinks rather than copies when symlinks are not the default for the platform |
--copies | Try to use copies rather than symlinks even when symlinks are the default for the platform |
--clear | Delete the contents of the environment directory, if it exists, before environment creation |
--upgrade | Upgrade the environment directory to use this version of Python, assuming that Python has been upgraded in-place |
--without-pip | This skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default) |
The most important argument to note is --system-site-packages, which enables the global site packages within the environment. This means that if you have a package installed in your global Python version, it will be available within your environment as well. However, if you try to update it to a different version, it will be installed locally. Whenever possible, I would recommend disabling the --system-site-packages flag because it gives you a simple environment without too many variables. A simple update of the system packages could break your virtual environment otherwise, but worse, there is no way to know which packages are needed locally and which ones are just installed for other purposes.
To enable this for an existing environment, you can simply run the environment creation command again, but this time adding the --system-site-packages flag to enable the global site packages.
To disable it again, you can simply run the environment creation command without the flag. This will keep the locally (within the environment) installed packages available but will remove the global packages from your Python scope.
When using virtualenvwrapper, this can also be done with the toggleglobalsitepackages command from within the activated environment.
The --symlinks and --copies arguments can generally be ignored, but it is important to know the difference. These arguments decide whether the files will be copied from the base python directory or whether they will be symlinked.
Symlinks are a Linux/Unix/Mac thing; instead of copying a file it creates a symbolic link that tells the system where to find the actual file.
By default, venv will try to symlink the files, and if that fails, it will fall back to copying. Since Windows Vista and Python 3.2, this is also supported on Windows, so unless you're using a very old system, you will most likely be using symlinks in your environment. The benefit of symlinks is that it saves disk space and stays in sync with your Python installation. The downside is that if your system's Python version undergoes an upgrade, it can break the packages installed within your environment, but that can easily be fixed by reinstalling the packages using pip.
Finally, the --upgrade argument is useful if your system Python version has been upgraded in-place. The most common use case for this argument is for repairing broken environments after upgrading the system Python with a copied (as opposed to symlinked) environment.