4

I am running this inside a virtual environment.

When I type:

pip install django

I get:

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: /home/ENV/env/lib/python3.5/site-packages/pytz Consider using the --user option or check the permissions.

For it to work, I actually need to input:

python -m pip install --user django

Why?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gorgonzola
  • 363
  • 3
  • 11
  • What Are You Using Window or Mac And Linux? – Hamza Lachi Nov 13 '19 at 06:23
  • if you are using windows window don't allow python to install django – Hamza Lachi Nov 13 '19 at 06:26
  • if you give me os name i will give you answer – Hamza Lachi Nov 13 '19 at 06:27
  • Possible duplicate of [What is the purpose "pip install --user ..."?](https://stackoverflow.com/questions/42988977/what-is-the-purpose-pip-install-user) – Sundeep Pidugu Nov 13 '19 at 07:10
  • @SundeepPidugu not really, you forgot the `python -m` part – RMPR Nov 13 '19 at 07:20
  • @RMPR Both of them work just fine. You can either use `pip` or `pip3` while running. – Sundeep Pidugu Nov 13 '19 at 07:26
  • 1
    This question also have his own importance because `python -m` is here to specify the interpreter, you must not neglect that, if you have multiple versions of python installed, keeping track of which python version version pip is bound to is a PITA imo, hence `python -m` in this case you're sure that it's the pip bound to the python called which will be executed. – RMPR Nov 13 '19 at 07:40
  • So it's a combination of [what-is-the-purpose-of-the-m-switch](https://stackoverflow.com/questions/7610001/what-is-the-purpose-of-the-m-switch) and your question. – RMPR Nov 13 '19 at 07:44
  • Got it @RMPR thanks – Sundeep Pidugu Nov 14 '19 at 05:50

3 Answers3

3

Let's break the two statements you added to your first command: python -m install --user django

  • python -m: Allows modules to be located using the Python module namespace for execution as scripts. The motivating examples were standard library modules such as pdb and profile. See PEP 338

  • --user : By default pip installs Python packages to system directories which requires root privileges, to avoid using sudo pip install (which is not recommended by the way) use this flag to make pip install packages in your home directory instead, which doesn't require any special privileges.

As a side note, if you have multiple versions of Python installed, keeping track of which Python version version pip is bound to can be a PITA, hence python -m in this case you're sure that it's the pip bound to the Python called which will be executed.

RMPR
  • 3,368
  • 4
  • 19
  • 31
1

This is because you are trying to install the package to a system folder which you don't have permissions to write to. And the option --user allows to install it to user folder, where you do have rights ([Errno 13] Permission denied. How do I solve this problem? #236).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kosist
  • 2,868
  • 2
  • 17
  • 30
0

The error basically states that you do not have permissions to write files to your machine, so if you are a root user (or administrator) you can always install Python packages with:

sudo pip install django

Or

pip install --user django

--user makes pip install packages in your home directory instead, which doesn't require any special privileges.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43