DEV Community

pO0q πŸ¦„
pO0q πŸ¦„

Posted on

Python env: be careful with requirements

It's not uncommon to provide a file called requirements.txt that list all third-party packages the script needs to work properly.

This way, users only have to use the following command before running the desired script:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

The technique relies on pip, the package manager for Python, and the requirements.txt file simply contains a list of all pip packages you have to install (one per line).

Check all dependencies carefully

It's pretty convenient and quite robust, but it's also prone to bad scenarios.

If the script you want to use relies on too much dependencies, it's not necessarily evil, but it's usually a bad sign.

This list should be kept short, in my opinion.

Use venv

You should use venv to create isolated virtual environments "with their own independent set of Python packages installed in their site directories."

source: Python documentation - venv

If you'd like to share your Python scripts or plan to maintain a library, you can include it in your documentation to let beginners know it's possible to install your stuff in a virtual env and not globally.

It does not require any installation, as venv is included in Python.

You will also avoid various compatibility issues, and a virtual env is easy to deactivate.

Wrap up

As a good practice, never install Python packages globally unless you perfectly know what you're doing.

Instead, you can leverage virtual environments to isolate your projects and don't mess with system-wide packages.

It won't solve all situations but you may avoid some headaches.

Top comments (0)