Is there a way to install a package for more than one venv without wasting space?

As title. I have many projects that uses more or less the same packages, and I would know if there’s a way to point to the same files without reinstalling them in every venv, wasting a lot of space (the requirements are very heavy).

(installing them globally is not an option)

One option is to use conda instead of venvs, since conda (by default) uses hardlinks. The actual files for a particular version of a particular package are only installed once and then each environment hardlinks to those same files. This means they only take up space on disk once (although some tools in some contexts may misleadingly report that the space is used multiple times).

2 Likes

Ty. This works also on Windows? Have I to run conda as admin?

Yes it works on windows, and you don’t need to run conda as admin.

Just make sure your environment is in the same partition as the package cache (which is the default), else it will copy instead of hardlink.

Alternatively, if you want to keep using venv, you can use uv pip instead of pip.
uv pip also uses hardlinks by default.

Again, make sure that the uv cache dir is in the same partition as your venv, or it will fall back to copy.

3 Likes

I tried it, but it seems to not work:

python3 -m venv test_uv_1
. test_uv_1/bin/activate
pip install uv
uv pip install pytest

than I checked test_uv_1/lib/python3.10/site-packages/pytest and its contents. They seems normal files and folders, not hardlinks

How are you checking? Your file explorer will just show them as normal files.
You would need to use other tools to see that two paths are hardlink to the same file.

See:

1 Like

Aaaah this is quite interesting. I never used hard links, only soft links, so I assumed they will appear the same in ll. But you are right, this can’t be because they are regular file that simply points to the same inode. ll -i shows it.

Ty a lot, I thought uv was “only” a faster pip. It seems on the contrary an invaluable tool. And ty also for the patience.

1 Like