How to separate packages for private use from relevant to the project in the same virtual environment?

Viewed 129

Here's a typical workflow when working with virtual environments:

1) Create virtual envinronment:

python3 -m venv venv

2) Activate it:

source venv/bin/activate

3) Install needed requirements:

(venv): pip install ...

4) (Write the code of the Package)

5) Freeze the code/Create the Package dependencies.

At the last point selected python tool wraps up all the packages from my virtual environment installed at point 3).

And here's my problem. What if some requirements installed at point 3) are just private packages used only by me for development purposes? For example, in order to have Spyder editor working in different virtual environments I need to install spyder-kernels. They are not needed for my Package, and they are not needed for other developers if they don't use this editor. However, when I

(venv): pip freeze > requirements.txt

I get requirements.txt with spyder-kernels package included in it, and then

(venv2): pip install -r requirements.txt

installs all the packages from requirements.txt unless I manually delete unwanted lines.

Is there any flag for pip install that could tell pip to install some packages as hidden/private for freezing/packaging process? Or any other automatic workaround that could achieve my goal? The perfect scenario would be something like this:

python3 -m venv venv
source venv/bin/activate
(venv): pip install important_packages
(venv): pip install -privateFLAG spyder-kernels
(venv): pip freeze > requirements.txt  # (stores important_packages)
(venv): pip freeze -privateFLAG > private_requirements.txt

and then one can choose to recreate the environment depenedencies with: '''pip install -r requirements.txt''' or '''pip install -r requirements.txt private_requirements.txt''' if he needs whatever in private_requirements.txt is.

Can we do some similar trick for freezing code?

2 Answers

I'm not aware of such functionality in pip.

When you install a "private" package, you could add said package to a file, for example private_requirements.txt. Then with a simple shell script you could filter the output of pip freeze in a way that if a package appears in private_requirements.txt then the matching line should be removed from freeze output.

It should not be hard to implement, for example you could use:

pip freeze | grep -v -f private_requirements.txt > requirements.txt
Related