Start using pipx!

Start using pipx!

When you start working with Python, it is most likely that you would use pip to install every other Python package without knowing the intricacies of Python's virtual environment.

In Python, virtual environments are isolated Python environments with their own Python interpreter and packages installed. Many beginner Python programmers ignore this concept and end up using the same Python environment for all of their work and projects. If you are the person who does this, maybe you have realized that after a few projects, your dependencies have conflicted with each other. This problem can range from minor inconvenience to huge dependency issues.

Now as an experienced programmer, you soon start creating virtual environments for each of your projects. Congratulations! You deserve a pat on the back. But after a few projects, you start noticing that you have a few pip dependencies that are common for all of your projects. These dependencies are not really necessary for running your application or scripts but are necessary during development time. Tools such as Pylinter, black, cookie-cutter, Jupyter Notebooks, poetry are some of the tools that you probably use for most of your projects. Sometimes you wonder whether you need to install all of these in each of your virtual environments.

Let me introduce you to pipx! Using pipx you can install Python modules in separate virtual environments that are globally callable. Every time you install the package using pipx, a new virtual environment is created for that package making it isolated.

You can install pipx using pip the following command:

python3 -m pip install --user pipx
python3 -m pipx ensurepath

Remember that pipx uses the virtual environment to manage different packages, you must make sure that virtualenv is installed. You might get an error regarding missing python-virtualenv when you start installing Python packages. You can find more details on installation here: https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/

Now you can use pipx command to install the Python packages. For example, black is one of the packages that I use most of the time to format my code. To install black globally using pipx you just run the following:

pipx install black

See! It is exactly similar to how you install packages using pip.

To use black you can just run from anywhere:

black main.py

pipx's documentation lists some packages to try out: https://pypa.github.io/pipx/programs-to-try/.