python-3.xinstallationpip

Why not to create virtual environment outside home directory (error: externally-managed-environment on virtualenv in pip python3 package installation)


I know many similar questions are there. but I am having this issue in virtual env. I am trying to install Pandas on my machine, I already have virtual environment in which I am trying to install Pandas with pip, couple of days ago I install tensorflow that worked fine, but now gives me error-

Here's what is happening-

rpi@rpi:~ $ cd /var/forjupyter/jupyter
rpi@rpi:/var/forjupyter/jupyter $ source env1/bin/activate
(env1) rpi@rpi:/var/forjupyter/jupyter $ pip3 install pandas
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pandas
Using cached pandas-2.2.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (15.6 MB)
 Requirement already satisfied: numpy>=1.23.2 in ./env1/lib/python3.11/site-packages (from pandas) (1.26.4)
Requirement already satisfied: python-dateutil>=2.8.2 in ./env1/lib/python3.11/site-packages (from pandas) (2.9.0.post0)
Collecting pytz>=2020.1
Using cached https://www.piwheels.org/simple/pytz/pytz-2024.1-py3-none-any.whl (505 kB)
Collecting tzdata>=2022.7
Using cached https://www.piwheels.org/simple/tzdata/tzdata-2024.1-py2.py3-none-any.whl (345 kB)
Requirement already satisfied: six>=1.5 in ./env1/lib/python3.11/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
Installing collected packages: pytz, tzdata, pandas
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '/var/forjupyter/jupyter/env1/lib/python3.11/site-packages/pytz'
Check the permissions.

When I try sudo gives me error-

 error: externally-managed-environment

  × This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.

If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.

For more information visit http://rptl.io/venv

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
Note: you may need to restart the kernel to use updated packages.

with apt install python3-pandas successful installation was done but when i try to access it in notebook on env1 Pandas is not found when i try notebook on main env tensorflow is not found what can do can I import Pandas to env1. Have same error as first in Jupyter Notebook with %pip or pip for installation of any package.


Solution

  • Question(issue) started as mentioned above in question but I have realized that if you create virtual environment outside home directory especially somewhere in place root access might get needed you are gonna get above error.

    What was happening is if you have env outside home and try to use pip you will get permission denied because you are not root, but if you use sudo it is assuming you are installing package system wide. So if you want to install something with no risk like break-system-packages create virtual env where user can access it with no special permissions

    Thats all.Thanks for support.