Something that really annoys me when using Python is creating a Virtual Environment (or venv) each time.

Starting with Python 3.12+ and pip 24, installing external packages like ansible (pip install ansible) globally is intentionally restricted on most systems unless you use --break-system-packages — because global installs can mess with system tools.

So to quickly create a persistent environment we can use a tool that creates and manages lightweight virtual environments automatically: pipx

General install

python3 -m pip install --user pipx

After installation you might need to add your home .local/bin folder location to the PATH environment variable:

python3 -m pipx ensurepath

Then you need to reload the shell:

source ~/.bashrc

Now if you try to install third-party packages it may succed or it may request to add the virtual environment package

The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt install python3.13-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Finally you can install external packages in a safe way

pipx install ansible
pipx install httpie

pipx environment are persistent and lives in the path ~/.local/pipx/venvs/

Naming

Generally pipx will name the environment after the package name we are installing, so pipx install ansible will create ~/.local/pipx/venvs/ansible

If we want to specifically name the environment, we can use the --suffix option to append an additional text to the package name. So for example:

pipx install ansible --suffix=-test1
installed package ansible 12.1.0 (ansible-test1), installed using Python 3.13.5
These apps are now globally available
    - ansible-community-test1    <---- This is important
done! 🌟

Will create ansible-test1 environment in ~/.local/pipx/venvs/ansible-test1/

Keep in mind

The output shows the environment name (the one we defined with the suffix), and the global app now available which aren’t the same thing.

General commands, like ansible --version in this case, will not work.

ansible --version
bash: ansible: command not found

To run the package commands you have to pay attention to the global app naming on the packages installation output.

For example we have ansible-community-test1 from the previous snippet.

ansible-community-test1 --version
Ansible community version 12.1.0

There are also other ways to quickly create virtual environment but pipx my fav one.


Official docs: https://pipx.pypa.io/stable/installation/