When you install a package using pip
, one common error that you may encounter is a permission error as follows:
Could not install packages due to an EnvironmentError: [Errno 13]
Permission denied: '/usr/lib/python3.5/dist-packages'
Consider using the `--user` option or check the permissions.
This error usually occurs when you don’t have the required permissions to install Python packages in the specified directory.
To solve this error, try adding the --user
option as recommended by pip.
Install Python packages using --user
option
The --user
flag is used to tell Python to install the package in the user’s home directory instead of the system directory.
You need to run the install command as follows:
pip install <package_name> --user
# for pip3
pip3 install <package_name> --user
# for windows
py install <package_name> --user
This way, the Python package is installed in a directory that’s accessible by the current user.
If that doesn’t work, you can try to use the sudo
command.
Install Python packages using sudo
The sudo
command gives you all system permissions, allowing you to run commands as the administrator.
Installing a package with sudo
should work, but you will also need to run the Python script using sudo
later.
This is how you install packages with sudo
:
sudo pip install <package_name>
# for pip3
sudo pip3 install <package_name>
# for windows
sudo py install <package_name>
Keep in mind that there’s no sudo
command for Windows-based OS.
You need to run the Terminal or Command Prompt as administrator instead:
Once the program is run as administrator, you can install the packages without adding sudo
or --user
command.
Conclusion
To conclude, the EnvironmentError that occurs because of lacking permissions can be solved in two ways:
- Adding the
--user
option to the install command - Add the
sudo
command or run the command prompt as administrator
I recommend you try the --user
option first before using the sudo
command, as it allows you to access the packages without having administrator permissions.
Good work solving this Python error! 👍