How to prevent anaconda environment from reading libraries installed in local - python

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']

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.

Related

Apache can't find mysql.connector when running Python cgi

I have a cgi that ran fine on python 2.7.
When I converted it to python3, I had to install some modules again. I installed requests by simply copying it from a 2.7 site-packages directory to /usr/lib/python3.7/site-packages.
I used pip to install mysql.connector, during which the installer said that since the global site-packages directory was not writable, it was installing it in my user directory instead.
The cgi ran fine when I ran it from the command line on the host, but when it is called as a cgi, I get:
import mysql.connector
ModuleNotFoundError: No module named 'mysql.connector'
So, apache can't find mysql.connector, even though it finds requests and everything else. I tried copying all the mysql* stuff from my local site-packages like I did with requests, but no joy.
Here's where python3 is looking for things when I'm running it as me:
(myusername)lab:/var/www/cgi-bin$ python3 -m site
sys.path = [
'/var/www/cgi-bin',
'/usr/lib64/python37.zip',
'/usr/lib64/python3.7',
'/usr/lib64/python3.7/lib-dynload',
'/home/myusername/.local/lib/python3.7/site-packages',
'/usr/lib64/python3.7/site-packages',
'/usr/lib/python3.7/site-packages',
]
USER_BASE: '/home/myusername/.local' (exists)
USER_SITE: '/home/myusername/.local/lib/python3.7/site-packages' (exists)
ENABLE_USER_SITE: True
Here's where I think apache is looking for things:
(myusername)lab:/var/www/cgi-bin$ sudo -u apache python3 -m site
sys.path = [
'/var/www/cgi-bin',
'/usr/lib64/python37.zip',
'/usr/lib64/python3.7',
'/usr/lib64/python3.7/lib-dynload',
'/usr/lib64/python3.7/site-packages',
'/usr/lib/python3.7/site-packages',
]
USER_BASE: '/usr/share/httpd/.local' (doesn't exist)
USER_SITE: '/usr/share/httpd/.local/lib/python3.7/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
Any guidance would be greatly appreciated.
Update:
I changed permissions for all the mysql* directories and their contents to be readable and executable by everybody, and that got rid of the error. But I don't know if that was the best solution. Seems like it would not be.

How do I change pipenv/virtualenv location to work with Portable VS Code and project on thumb drive?

On Windows 10, I'm running VS Code 1.48.2 and Python 3, both installed on a thumb drive. I added an F:\Programs\VS Code\data folder with user-data andextensions. My project directory is F:\MyProject and it has a Pipfile. My project directory has a .env file and .venv\ so that pipenv will install to the local .venv\. What else do I need to get this environment working? Thanks!
.env file:
PYTHONPATH="F:\\Python\\Python38\\python.exe"
PYTHONHOME="F:\\MyProject\\.venv\\Scripts"
PIPENV_VENV_IN_PROJECT=True
F:\MyProject\.vscode\settings.json file:
{
"python.pythonPath": "f:\\MyProject\\.venv\\Scripts\\python.exe"
}
Although, when I used Preferences: Open Settings, it opened my F:\Programs\VS Code\data\user-data\settings.json file, though the paths look right (to me):
{
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"python.languageServer": "Microsoft",
"python.pythonPath": "${workspacefolder}/.venv/Scripts/python.exe",
"python.venvPath": "${workspacefolder}/.venv",
"python.venvFolders": [
".venv",
"${workspacefolder}/.venv"
]
}
Used the following steps to setup:
ps > cd F:\MyProject
ps > F:\Programs\VS Code\code.exe .
[In vscode integrated terminal]
PS F:\MyProject> py -m venv --system-site-packages .venv
PS F:\MyProject> pipenv shell
Loading .env environment variables…
Warning: Your Pipfile requires python_version 3.8, but you are using unknown (F:\M\.venv\S\python.exe).
$ pipenv --rm and rebuilding the virtual environment may resolve the issue.
$ pipenv check will surely fail.
Launching subshell in virtual environment…
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS F:\MyProject> ls .\.venv\Scripts\python.exe
Directory: F:\MyProject\.venv\Scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/6/20 3:04 PM 532040 python.exe
PS F:\MyProject> python where
Python path configuration:
PYTHONHOME = 'F:\MyProject\.venv\Scripts'
PYTHONPATH = 'F:\Python\Python38\python.exe'
program name = 'F:\Python\Python38\python.exe'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'F:\\Python\\Python38\\python.exe'
sys.base_prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.base_exec_prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.executable = 'F:\\MyProject\\.venv\\Scripts\\python.exe'
sys.prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.exec_prefix = 'F:\\MyProject\\.venv\\Scripts'
sys.path = [
'F:\\Python\\Python38\\python.exe',
'F:\\Python\\Python38\\python38.zip',
'F:\\MyProject\\.venv\\Scripts\\DLLs',
'F:\\MyProject\\.venv\\Scripts\\lib',
'F:\\Python\\Python38',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x000020d4 (most recent call first):
<no Python frame>
PS F:\MyProject>
According to your description, you could refer to the following steps to use the virtual environment:
Open this project in VSCode.
According to the information you provided, there is already a virtual environment ".venv" in your project, so there will be a ".venv" folder after opening. like this:
Click "select Python Interpreter" in the lower left corner to select the ".venv" virtual environment:
VSCode has selected the virtual environment, and then we open a new terminal console through the shortcut key "Ctrl+Shift+`", VSCode will automatically enter the current virtual environment:
Use pip to install the module "PySimpleGui": (pip install PySimpleGui)
When we run the python script, VSCode showed that the module PySimpleGui could not be found. We found "Lib\site-packages" in the ".venv" folder of the project and changed "PySimpleGUI" to "PySimpleGui":
The python script runs successfully in the virtual environment:

Problem with accessing virtual environment Python from R Markdown

Note: I'm on Windows using Git Bash.
So, I am trying to setup a dev environment for the work for my class. It is going to involve a combination of coding in R and Python.
I created virtual environments using pipenv and virtualenv and ran into the same problem with both. So, first, let's create a virtual environment for the project in a sub-folder dev_env:
cd project_folder/dev_env
pipenv --python 3.7
pipenv --py
Output
C:\Users\Ra Me\.virtualenvs\dev_env-5TUtSZI9\Scripts\python.exe
Now I'm going into my file.rmd and trying the reticulate package.
#install.packages("reticulate")
library(reticulate)
Next, I tried 2 methods:
Sys.setenv(RETICULATE_PYTHON = "C:/Users/Ra Me/.virtualenvs/dev_env-5TUtSZI9/Scripts")
or
use_virtualenv("C:/Users/Ra Me/.virtualenvs/dev_env-5TUtSZI9/", required = TRUE)
x = 1
if x:
print('Hello!')
Both of them produced the error
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
However, when I change the path to the Python environment that's installed for all users on my machine, it works.
Sys.setenv(RETICULATE_PYTHON = "C:/Program Files/Python37/")
This method also works. However, here we are not even using the reticulate project.
knitr::opts_chunk$set(engine.path = list(
python = "C:/Program Files/Python37/python.exe",
r = "C:/Program Files/R/R-3.6.1/bin/R.exe"
))

How to give priority to conda package over pip one?

With my virtual environment activated, I see with conda list that my pandas version is 0.24.0. When I do the same with pip list, I see the version is 0.22.0 (probably an older version that I installed before using conda). When I import pandas in python (3.6), the pandas version is 0.22.0.
Why and how to force the loading of the conda package?
EDIT: MacOS High Sierra 10.13.1
TL;DR is in Possible Fix at the bottom
A few notes, and these may or may not answer the question, but I think this is a bit better than dumping everything into comments. These assume that your environment is activated, for these examples, my environment is called new36. I am also on MacOS with High Sierra 10.13.6.
Checking conda vs pip locations
First, let's check to make sure conda and pip are both looking in the same environment. To find information surrounding conda, check:
conda info
I get the following:
active environment : new36
active env location : /Users/mm92400/anaconda3/envs/new36
shell level : 1
user config file : /Users/mm92400/.condarc
populated config files : /Users/mm92400/.condarc
conda version : 4.6.8
conda-build version : 3.0.27
python version : 3.6.3.final.0
# extra info excluded
The active env location is what we're concerned with. This should be a directory that contains the directory of pip:
which pip | head -n 1
/Users/mm92400/anaconda3/envs/new36/bin/pip
If pip does not sit in a directory under where conda lives, this could be part of the issue.
Verifying the import path of python
You should be able to check where python is sourcing files from via sys.path:
import sys
sys.path
['', '/Users/mm92400/anaconda3/envs/new36/lib/python36.zip', '/Users/mm92400/anaconda3/envs/new36/lib/python3.6', '/Users/mm92400/anaconda3/envs/new36/lib/python3.6/lib-dynload', '/Users/mm92400/anaconda3/envs/new36/lib/python3.6/site-packages']
This is a list, and that's important to note. Note how my sys.path does not have any directories that source from a file/folder based on a base install of conda, nor any of the Framework installs of python on my Mac. import will search these directories ('' is cwd) in order, pulling down the first instance of a package that it finds. If your sys.path has an element earlier than your conda env that contains pandas, this is your problem.
Verbose python
You can also verify where the pandas package is being sourced from using the verbose mode of python, python -v:
# you have gotten here by running python -v in the terminal
# there's a whole bunch of comments that pop out that I'm going to omit here
# Now run
import pandas
~snip~
# code object from '/Users/mm92400/anaconda3/envs/new36/lib/python3.6/site-packages/pandas/__pycache__/_version.cpython-36.pyc'
import 'pandas._version' # <_frozen_importlib_external.SourceFileLoader object at 0x107952b00>
import 'pandas' # <_frozen_importlib_external.SourceFileLoader object at 0x104572b38>
Note how the code object path matches where I expect python to source that package from
Possible Fix
You can hack on sys.path, though I'm not sure how recommended that is. You can prioritize where directories are in sys.path without modifying sys.path in your script like:
env PYTHONPATH=$(find $CONDA_PREFIX -type d -name "site-packages" | head -n 1) python
which will take you into an interpreter and sys.path will look like:
import sys
sys.path
['', '/Users/mm92400/anaconda3/envs/new36/lib/python3.6/site-packages', ...]
Where now the first directory it will check is the conda env site-packages. Because sys.path is a list, it will be traversed in order. The way to prioritize which one you want to use is by injecting that particular directory into the sys.path first. If I were to write a script like:
import sys
print(f"I prioritized {sys.path[1]}")
And ran it using env PYTHONPATH=$(find $CONDA_PREFIX -type d -name "site-packages" | head -n 1) python somefile.py I would get:
env PYTHONPATH=$(find $CONDA_PREFIX -type d -name "site-packages" | head -n 1) python somefile.py
I prioritized /Users/mm92400/anaconda3/envs/new36/lib/python3.6/site-packages
Alternatively, you can insert into sys.path, but I can say definitively that this is not recommended and quite fragile:
import os, sys
try:
conda_env = os.environ['CONDA_PREFIX']
except KeyError:
raise KeyError("The env var $CONDA_PREFIX was not found. Please check that your conda environment was activated")
for root, dirs, files in os.walk(conda_env):
if 'site-packages' in dirs:
syspath_add = os.path.join(root, 'site-packages')
break
else:
raise FileNotFoundError("Couldn't find site-packages!")
sys.path.insert(0, syspath_add)
sys.path
# ['/Users/mm92400/anaconda3/envs/new36/lib/python3.6/site-packages', '', ...]

How do I find the location of my Python site-packages directory?

How do I find the location of my site-packages directory?
There are two types of site-packages directories, global and per user.
Global site-packages ("dist-packages") directories are listed in sys.path when you run:
python -m site
For a more concise list run getsitepackages from the site module in Python code:
python -c 'import site; print(site.getsitepackages())'
Caution: In virtual environments getsitepackages is not available with older versions of virtualenv, sys.path from above will list the virtualenv's site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:
python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'
The per user site-packages directory (PEP 370) is where Python installs your local packages:
python -m site --user-site
If this points to a non-existing directory check the exit status of Python and see python -m site --help for explanations.
Hint: Running pip list --user or pip freeze --user gives you a list of all installed per user site-packages.
Practical Tips
<package>.__path__ lets you identify the location(s) of a specific package: (details)
$ python -c "import setuptools as _; print(_.__path__)"
['/usr/lib/python2.7/dist-packages/setuptools']
<module>.__file__ lets you identify the location of a specific module: (difference)
$ python3 -c "import os as _; print(_.__file__)"
/usr/lib/python3.6/os.py
Run pip show <package> to show Debian-style package information:
$ pip show pytest
Name: pytest
Version: 3.8.2
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT license
Location: /home/peter/.local/lib/python3.4/site-packages
Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy
>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
(or just first item with site.getsitepackages()[0])
A solution that:
outside of virtualenv - provides the path of global site-packages,
insidue a virtualenv - provides the virtualenv's site-packages
...is this one-liner:
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
Formatted for readability (rather than use as a one-liner), that looks like the following:
from distutils.sysconfig import get_python_lib
print(get_python_lib())
Source: an very old version of "How to Install Django" documentation (though this is useful to more than just Django installation)
For Ubuntu,
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
...is not correct.
It will point you to /usr/lib/pythonX.X/dist-packages
This folder only contains packages your operating system has automatically installed for programs to run.
On ubuntu, the site-packages folder that contains packages installed via setup_tools\easy_install\pip will be in /usr/local/lib/pythonX.X/dist-packages
The second folder is probably the more useful one if the use case is related to installation or reading source code.
If you do not use Ubuntu, you are probably safe copy-pasting the first code box into the terminal.
This is what worked for me:
python -m site --user-site
A modern stdlib way is using sysconfig module, available in version 2.7 and 3.2+. Unlike the current accepted answer, this method still works regardless of whether or not you have a virtual environment active.
Note: sysconfig (source) is not to be confused with the distutils.sysconfig submodule (source) mentioned in several other answers here. The latter is an entirely different module and it's lacking the get_paths function discussed below. Additionally, distutils is deprecated in 3.10 and will be unavailable soon.
Python currently uses eight paths (docs):
stdlib: directory containing the standard Python library files that are not platform-specific.
platstdlib: directory containing the standard Python library files that are platform-specific.
platlib: directory for site-specific, platform-specific files.
purelib: directory for site-specific, non-platform-specific files.
include: directory for non-platform-specific header files.
platinclude: directory for platform-specific header files.
scripts: directory for script files.
data: directory for data files.
In most cases, users finding this question would be interested in the 'purelib' path (in some cases, you might be interested in 'platlib' too). The purelib path is where ordinary Python packages will be installed by tools like pip.
At system level, you'll see something like this:
# Linux
$ python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))"
/usr/local/lib/python3.8/site-packages
# macOS (brew installed python3.8)
$ python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))"
/usr/local/Cellar/python#3.8/3.8.3/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
# Windows
C:\> py -c "import sysconfig; print(sysconfig.get_path('purelib'))"
C:\Users\wim\AppData\Local\Programs\Python\Python38\Lib\site-packages
With a venv, you'll get something like this
# Linux
/tmp/.venv/lib/python3.8/site-packages
# macOS
/private/tmp/.venv/lib/python3.8/site-packages
# Windows
C:\Users\wim\AppData\Local\Temp\.venv\Lib\site-packages
The function sysconfig.get_paths() returns a dict of all of the relevant installation paths, example on Linux:
>>> import sysconfig
>>> sysconfig.get_paths()
{'stdlib': '/usr/local/lib/python3.8',
'platstdlib': '/usr/local/lib/python3.8',
'purelib': '/usr/local/lib/python3.8/site-packages',
'platlib': '/usr/local/lib/python3.8/site-packages',
'include': '/usr/local/include/python3.8',
'platinclude': '/usr/local/include/python3.8',
'scripts': '/usr/local/bin',
'data': '/usr/local'}
A shell script is also available to display these details, which you can invoke by executing sysconfig as a module:
python -m sysconfig
Addendum: What about Debian / Ubuntu?
As some commenters point out, the sysconfig results for Debian systems (and Ubuntu, as a derivative) are not accurate. When a user pip installs a package it will go into dist-packages not site-packages, as per Debian policies on Python packaging.
The root cause of the discrepancy is because Debian patch the distutils install layout, to correctly reflect their changes to the site, but they fail to patch the sysconfig module.
For example, on Ubuntu 20.04.4 LTS (Focal Fossa):
root#cb5e85f17c7f:/# python3 -m sysconfig | grep packages
platlib = "/usr/lib/python3.8/site-packages"
purelib = "/usr/lib/python3.8/site-packages"
root#cb5e85f17c7f:/# python3 -m site | grep packages
'/usr/local/lib/python3.8/dist-packages',
'/usr/lib/python3/dist-packages',
USER_SITE: '/root/.local/lib/python3.8/site-packages' (doesn't exist)
It looks like the patched Python installation that Debian/Ubuntu are distributing is a bit hacked up, and they will need to figure out a new plan for 3.12+ when distutils is completely unavailable. Probably, they will have to start patching sysconfig as well, since this is what pip will be using for install locations.
Let's say you have installed the package 'django'. import it and type in dir(django). It will show you, all the functions and attributes with that module. Type in the python interpreter -
>>> import django
>>> dir(django)
['VERSION', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'get_version']
>>> print django.__path__
['/Library/Python/2.6/site-packages/django']
You can do the same thing if you have installed mercurial.
This is for Snow Leopard. But I think it should work in general as well.
As others have noted, distutils.sysconfig has the relevant settings:
import distutils.sysconfig
print distutils.sysconfig.get_python_lib()
...though the default site.py does something a bit more crude, paraphrased below:
import sys, os
print os.sep.join([sys.prefix, 'lib', 'python' + sys.version[:3], 'site-packages'])
(it also adds ${sys.prefix}/lib/site-python and adds both paths for sys.exec_prefix as well, should that constant be different).
That said, what's the context? You shouldn't be messing with your site-packages directly; setuptools/distutils will work for installation, and your program may be running in a virtualenv where your pythonpath is completely user-local, so it shouldn't assume use of the system site-packages directly either.
The native system packages installed with python installation in Debian based systems can be found at :
/usr/lib/python2.7/dist-packages/
In OSX - /Library/Python/2.7/site-packages
by using this small code :
from distutils.sysconfig import get_python_lib
print get_python_lib()
However, the list of packages installed via pip can be found at :
/usr/local/bin/
Or one can simply write the following command to list all paths where python packages are.
>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
Note: the location might vary based on your OS, like in OSX
>>> import site; site.getsitepackages()
['/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python', '/Library/Python/2.7/site-packages']
pip show will give all the details about a package:
https://pip.pypa.io/en/stable/reference/pip_show/ [pip show][1]
To get the location:
pip show <package_name>| grep Location
In Linux, you can go to site-packages folder by:
cd $(python -c "import site; print(site.getsitepackages()[0])")
All the answers (or: the same answer repeated over and over) are inadequate. What you want to do is this:
from setuptools.command.easy_install import easy_install
class easy_install_default(easy_install):
""" class easy_install had problems with the fist parameter not being
an instance of Distribution, even though it was. This is due to
some import-related mess.
"""
def __init__(self):
from distutils.dist import Distribution
dist = Distribution()
self.distribution = dist
self.initialize_options()
self._dry_run = None
self.verbose = dist.verbose
self.force = None
self.help = 0
self.finalized = 0
e = easy_install_default()
import distutils.errors
try:
e.finalize_options()
except distutils.errors.DistutilsError:
pass
print e.install_dir
The final line shows you the installation dir. Works on Ubuntu, whereas the above ones don't. Don't ask me about windows or other dists, but since it's the exact same dir that easy_install uses by default, it's probably correct everywhere where easy_install works (so, everywhere, even macs). Have fun. Note: original code has many swearwords in it.
An additional note to the get_python_lib function mentioned already: on some platforms different directories are used for platform specific modules (eg: modules that require compilation). If you pass plat_specific=True to the function you get the site packages for platform specific packages.
This works for me.
It will get you both dist-packages and site-packages folders.
If the folder is not on Python's path, it won't be
doing you much good anyway.
import sys;
print [f for f in sys.path if f.endswith('packages')]
Output (Ubuntu installation):
['/home/username/.local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages']
This should work on all distributions in and out of virtual environment due to it's "low-tech" nature. The os module always resides in the parent directory of 'site-packages'
import os; print(os.path.dirname(os.__file__) + '/site-packages')
To change dir to the site-packages dir I use the following alias (on *nix systems):
alias cdsp='cd $(python -c "import os; print(os.path.dirname(os.__file__))"); cd site-packages'
A side-note: The proposed solution (distutils.sysconfig.get_python_lib()) does not work when there is more than one site-packages directory (as recommended by this article). It will only return the main site-packages directory.
Alas, I have no better solution either. Python doesn't seem to keep track of site-packages directories, just the packages within them.
from distutils.sysconfig import get_python_lib
print get_python_lib()
You should try this command to determine pip's install location
Python 2
pip show six | grep "Location:" | cut -d " " -f2
Python 3
pip3 show six | grep "Location:" | cut -d " " -f2
Answer to old question. But use ipython for this.
pip install ipython
ipython
import imaplib
imaplib?
This will give the following output about imaplib package -
Type: module
String form: <module 'imaplib' from '/usr/lib/python2.7/imaplib.py'>
File: /usr/lib/python2.7/imaplib.py
Docstring:
IMAP4 client.
Based on RFC 2060.
Public class: IMAP4
Public variable: Debug
Public functions: Internaldate2tuple
Int2AP
ParseFlags
Time2Internaldate
For those who are using poetry, you can find your virtual environment path with poetry debug:
$ poetry debug
Poetry
Version: 1.1.4
Python: 3.8.2
Virtualenv
Python: 3.8.2
Implementation: CPython
Path: /Users/cglacet/.pyenv/versions/3.8.2/envs/my-virtualenv
Valid: True
System
Platform: darwin
OS: posix
Python: /Users/cglacet/.pyenv/versions/3.8.2
Using this information you can list site packages:
ls /Users/cglacet/.pyenv/versions/3.8.2/envs/my-virtualenv/lib/python3.8/site-packages/
I made a really simple function that gets the job done
import site
def get_site_packages_dir():
return [p for p in site.getsitepackages()
if p.endswith(("site-packages", "dist-packages"))][0]
get_site_packages_dir()
# '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages'
If you want to retrieve the results using the terminal:
python3 -c "import site;print([p for p in site.getsitepackages() if p.endswith(('site-packages', 'dist-packages')) ][0])"
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages
I had to do something slightly different for a project I was working on: find the relative site-packages directory relative to the base install prefix. If the site-packages folder was in /usr/lib/python2.7/site-packages, I wanted the /lib/python2.7/site-packages part. I have, in fact, encountered systems where site-packages was in /usr/lib64, and the accepted answer did NOT work on those systems.
Similar to cheater's answer, my solution peeks deep into the guts of Distutils, to find the path that actually gets passed around inside setup.py. It was such a pain to figure out that I don't want anyone to ever have to figure this out again.
import sys
import os
from distutils.command.install import INSTALL_SCHEMES
if os.name == 'nt':
scheme_key = 'nt'
else:
scheme_key = 'unix_prefix'
print(INSTALL_SCHEMES[scheme_key]['purelib'].replace('$py_version_short', (str.split(sys.version))[0][0:3]).replace('$base', ''))
That should print something like /Lib/site-packages or /lib/python3.6/site-packages.
Something that has not been mentioned which I believe is useful, if you have two versions of Python installed e.g. both 3.8 and 3.5 there might be two folders called site-packages on your machine. In that case you can specify the python version by using the following:
py -3.5 -c "import site; print(site.getsitepackages()[1])

Categories