Newbie trying to understand pip and virtual environment

Short time ago, I started with PYTHON and have few applications running. Up to now, I did not take into account, if pip packages have been installed from repository or using pip install.
Actually I’m thinking to clean up in order to use pipx from now on. pip freeze shows 205 packages, pip list additionally:

pip        23.1.2
setuptools 65.5.0
wheel      0.40.0

How should I proceed? Thank you for your advice!

I don’t follow the question. Couldn’t you pip list then pip uninstall ...?

As far as virtual environments - use them to isolate package versions to projects.

Basically - build each project in its own virtual environment so that it’s dependencies are isolated.

I use Poetry for my workflow. It creates a virtualenv for the project automatically.

Thank you Steven @swills1

You mean to uninstall the ~ 200 pip-packages and start from scratch?
I’d appreciate also an advice to setup and use virtualenv for different projects (PYTHON applications).

I’m a venv/pip person and haven’t used pipx.

My own approach is basicly this:

 cd my_project_dir
 python3 -m venv venv    # make an empty virtualenv in "venv"
                         # pick your preferred subdir name
 ./venv/bin/python3 -m pip install ...whatever...

Whenever you invoke ./venv/bin/python3 it runs the Python associated
with the virtual environment you made just here, unique to this project.
This is what Steven meant about “isolated”: a distinct venv per project
with just what you wanted for that project.

Personally, I don’t use the “activate” command (which fiddles your shell
environment), because I only want the venv Python when running the
project code, not for general stuff. So I’ve got a 'dev" command which:

  • prepends the project Python source tree to $PYTHONPATH
  • prepends ./venv/bin to $PATH (with the full path to venv)
  • execs its arguments

So when I want to run the project Python I do something like:

 dev python3 ...whatever...

which will find the python3 from the venv.

Typically projects keep their required third party packages in a
requirements.txt file; that both documents them and also means you can
reinstall the packages for yourproject in the venv with:

 dev python3 -m pip install -r requirements.txt

(adjust to suit).

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Specifically what problem are you trying to solve?

Does this cause a problem? Why/how?

What exactly are you “trying to understand” here?

1 Like