Python tries to read a library installed under ~/.local
, even though I am working on an anaconda environment.
> conda create -n testproj python=3.6
> conda activate testproj
> conda install pandas
> python
>>> import pandas as pd
Then I got an ImportError
ImportError: C extension: /home/myname/.local/lib/python3.6/site-packages/pandas/ ...
But if I change the permission of site-packages
> chmod 000 ~/.local/lib/python3.6/site-packages
Then I can import pandas
without any error. Namely Python is looking at outside of the anaconda environment.
Question: How can I prevent Python from reading libraries outside the anaconda environment?
Environment: openSUSE Leap 15.0
EDIT: I found that sys.path
contains site-packages
under ~/.local
. I do not think that the lines should be there.
['',
'/home/myname/anaconda3/envs/myproj/bin',
'/home/myname/anaconda3/envs/myproj/lib/python36.zip',
'/home/myname/anaconda3/envs/myproj/lib/python3.6',
'/home/myname/anaconda3/envs/myproj/lib/python3.6/lib-dynload',
'/home/myname/.local/lib/python3.6/site-packages',
'/home/myname/anaconda3/envs/myproj/lib/python3.6/site-packages',
'/home/myname/.local/lib/python3.6/site-packages/IPython/extensions',
'/home/myname/.ipython']
You may change the default location by using the following command but it is not encouraged. Conda can no longer find your environment by your environment name, you will have to specify the environment's full path to activate it every time.
Find package dependencies. By default, Anaconda/Miniconda stores packages in ~/anaconda/pkgs/ (or ~/opt/pkgs/ on macOS Catalina).
Open anaconda prompt & use 'conda env list' to find the location of the environment you wish to use. Go to the start menu, right-click 'Anaconda Prompt' and go to file location. Open its properties & change the target to the location of your preferred environment.
I get the same behavior on windows, clean environments include your user local packages. This is an open issue: https://github.com/conda/conda/issues/7173. conda
doesn't support doing what you're asking directly (yet).
You can always just set the environment variable PYTHONNOUSERSITE
(to any value), or invoke your interpreter with the -s
switch, and you wont get your local packages (~/.local
on windows is C:\Users\<username>\AppData\Roaming\Python\Python36\site-packages
):
(test-env) C:\Users\matt>python -m site
sys.path = [
'C:\\Users\\matt',
'C:\\Anaconda440\\envs\\test-env\\python36.zip',
'C:\\Anaconda440\\envs\\test-env\\DLLs',
'C:\\Anaconda440\\envs\\test-env\\lib',
'C:\\Anaconda440\\envs\\test-env',
'C:\\Users\\matt\\AppData\\Roaming\\Python\\Python36\\site-packages',
'C:\\Users\\matt\\AppData\\Roaming\\Python\\Python36\\site-packages\\some_lib-1.0-py3.6.egg',
'C:\\Anaconda440\\envs\\test-env\\lib\\site-packages',
]
USER_BASE: 'C:\\Users\\matt\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\matt\\AppData\\Roaming\\Python\\Python36\\site-packages' (exists)
ENABLE_USER_SITE: True
versus (note the -s
switch, and now my local packages are no longer on my sys.path
):
(test-env) C:\Users\matt>python -s -m site
sys.path = [
'C:\\Users\\matt',
'C:\\Anaconda440\\envs\\test-env\\python36.zip',
'C:\\Anaconda440\\envs\\test-env\\DLLs',
'C:\\Anaconda440\\envs\\test-env\\lib',
'C:\\Anaconda440\\envs\\test-env',
'C:\\Anaconda440\\envs\\test-env\\lib\\site-packages',
]
USER_BASE: 'C:\\Users\\matt\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\matt\\AppData\\Roaming\\Python\\Python36\\site-packages' (exists)
ENABLE_USER_SITE: False
HTH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With