sudo pip install…

I know that sudo pip is obviously wrong, so what am I supposed to do?

There are other issues as well, not using virtual environments and installations that break OS dependencies, and/or a mishmash of install options to begin with (like: do you use macports or homebrew? What happens when the tool you need is in one and not the other? why can’t they coincide peacefully together? Then managing what was installed from original sources and binaries, or through third parties such as homebrew and ports? And that’s not to mention installers built on the underlying technologies you’re trying to install!)

One issue I keep getting is about privileges and permissions and sudo seems to fix all that, but it can lead to other issues and break things as well, like the underlying OS reliance on older python and permissions for other users (not so bad in my single user environment /me thinks).

After playing with other technologies I am beginning to see an underlying pattern that virtual environments are a step in the right direction.

A user solution:

A much better and secure way would be:

pip install --user numpy # libs will be installed in ~/.local/lib

This is better, and can be used for installing applications, but it doesn’t solve the problem of having different versions needs for different python projects. Enter pipenvpipenv is to python what composer is to PHP. It lets you easily install and use libraries per project. It’s not the only tool allowing you to do that, but it’s the one I use so it’s the one I’m gonna present you. Example:

A pipenv solution

pipenv install numpy matplotlib pandas
# to start your program
pipenv run ./crunch-data.py
# to install libs from another machine, after a git pull:
pipenv sync
# to get a shell in the env (like `source myenv/bin/activate` for venv)
pipenv shell

This allows a very reproducible environment for your program, without resorting to Docker and without messing up user or system libraries. Save yourself from future bugs and start using pipenv, venv, conda or virtualenv right away! It’s much better than requirements.txt + pip. :)

reference: https://dev.to/elabftw/stop-using-sudo-pip-install-52mn

Leave a comment