Why can't I do something without sudo using Python and pip?

When I use pip, it usually does not work without sudo. I often see people using pip without sudo, so what am I doing wrong?

I read that it is not recommended to install pip packages with sudo. I know that with virtualenv I can use pip without sudo, but to install virtualenv I need to use sudo first.

When I try to install pip without sudo, I get:

 PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.4/dist-packages/pip' 

When trying to install a flask using pip3 install flask :

 PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.4/dist-packages/werkzeug' 
+6
source share
2 answers

sudo used on Unix / Linux systems to perform tasks as another user using their permissions, such as being able to write to some directories. If you do not specify a user to emulate, for example, at startup

 sudo pip install flask 

You are trying to run this command as a system administrator, known as root in many environments. You will be prompted to enter an administrator password (which can be your own if you have granted your user administrator privileges), then the specified command is launched as this user, which means that he has read / write access to almost every file and directory in the system ( there are some exceptions, but they are mostly angular and not very important here). This means that when using sudo you need to be especially careful, as the error is as small as simple space can really mess up: compare

 sudo rm -rf /usr/local/lib/python3.4/dist-packages/numpy* 

with

 sudo rm -rf /usr /local/lib/python3.4/dist-packages/numpy* 

See this space between /usr and local/ ? You have just begun to delete the entire /usr folder, which contains most of the vital files and programs in the system. Hope you have a backup! Now this does not mean that you need to be scared to death sudo , but you need to respect it.

Python installations are system-level (yes, I know there are exceptions), which means that you need to use sudo to modify them, for example, when installing third-party modules using pip . If you run

 ls -l /usr/local/lib/python3.4 

you will see something in the lines

 drwxrwsr-x 125 root 4096 Nov 3 00:40 dist-packages 

showing that the directory you are trying to install with pip is root , so you must use sudo .

Now, there are several ways around this. If you like it and don’t mind changing global system packages, continue to use sudo with pip (in fact, you may need to use sudo -H ... if you get a small yellow message at the beginning about permissions in your home directory). All your modules will be installed on /usr/local/lib/python3.4/dist-packages and will be available to all users on the system.

The second option is to use the --user parameter, which will create the lib/python3.4/site-packages hierarchy in your home directory ( ~ ) and save there all installed modules along with the scripts in ~/bin (which you must add to your $PATH The advantage of this method is that you do not need to use sudo , so you will not accidentally overwrite system-dependent modules where certain versions are required to run other programs.The disadvantage is that the installed modules are available only to you, so you may run into problems esl For example, your Web server is trying to run Flask himself, and can not read the original fayly.Odnako nothing that a small edit config file can not ispravit.Eto my recommended solution for most users.

The third option is to use virtual environments such as virtualenv . This will create a custom Python installation in your chosen location using a separate python executable and site-packages hierarchy (there are options for whether you want to reference the dist-packages repository or use it). You can pip install packages directly in virtualenv and create as many environments as your little hearts wish, each of which has slightly different versions of different dependencies, for example, so that you can test your programs more efficiently. You can turn virtual environments on and off, for example, for example, you could use a pair on different tabs of the terminal, for example, test in parallel. This is my second recommendation, because there (a little) more work is involved in activating and using the environment, and you can get confused about who you work with if you don't name them very well. Disadvantages include the lack of availability in the system, as well as the second option, and the fact that the virtual environment must be activated manually before use.

So, take a look at the options and see what works best for your system and your specific situation. Good luck

+6
source

The reason is that your regular user does not have the permissions necessary to change system directories. As in this post:

 PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.4/dist-packages/pip' 

Here is a summary of what you need to know:

You have python installed on your system, in order to change the system python, you must use sudo or be the root user.

You can install python libraries in your home directory without using sudo, but only you (and not other users of the system) can use it. Do this with pip install --user package-name , as mentioned by gongzhitaao.

You can also create unique python installations in a directory of your choice, for example, The Laughing Man. This is called virtualenv, and I think this is the most preferred way of working.

+9
source

All Articles