Tags


Running python scripts using different package versions

First written onNovember 10, 2019
Last updated onOctober 8, 2022

Introduction #

Installing Python packages from pacman as well as pip, using the root user, is not a good idea because pacman cannot keep track of files written by pip in the installation directories. File conflicts are easy to come out.

This problem stands out especially when using Python 3 packages installed from AUR because of their lack of maintenance. In this case I was tempted to run pip3 install ${PACKAGE}

Have a look at this article which is released under the Creative Commons Attribution-ShareAlike 4.0 International License by László Kiss Kollár.

Solutions #

Just as the original article explains, I suggest two methods:

  1. use the user installation method if you just need the executable
  2. use the virtual environment method if you need to import a module for a script.

The only problem of these two systems is that package updates need to be handled manually.

User installation method #

Add the following to your shell’s configuration (with GNU Bash use ~/.bashrc):

export PATH=$HOME/.local/bin:$PATH

Reload your shell and run:

pip3 install "${PACKAGE_NAME}" --user

You can now call the executable just like you would do after installing a package using the root user (# pip3 install "${PACKAGE_NAME}")

Virtual environment method #

Install virtualenv and then create a new virtual environment:

python3 -m venv ~/.local/venv/"${ENVIRONMENT_NAME}"
. ~/.local/venv/"${ENVIRONMENT_NAME}"/bin/activate
pip3 install ${YOUR_PACKAGES}
deactivate

To run a script using the newly created virtual environment you must call the virtual interpreter directly:

~/.local/venv/"${ENVIRONMENT_NAME}"/bin/python3 "${SCRIPT_PATH}"

Finally, add the path of the virtual environment executables to the shell’s path:

export PATH=$PATH:$HOME/.local/venv/"${ENVIRONMENT_NAME}"/bin

Reload your shell to be able to call a virtual environment executable directly.

Conclusion #

Don’t mess up your Python installation and have fun :)