Python Library Locations - How to find
By JoeVu, at: March 13, 2024, 11:58 a.m.
In the dynamic Python community, users often grapple with perplexing issues such as "executable not found" errors and discrepancies between PyCharm and the command prompt. This article aims to transcend mere problem-solving by delving into the fundamental aspects of how Python locates packages. Moreover, we'll explore the intricacies of library storage when employing virtual environments as virtualenv
, pyenv
, and pipenv
.
1. Understanding Python Library Locations
How Python Finds Packages:
Upon executing Python scripts or entering the interactive environment, the interpreter navigates through default paths for package location. It crucially considers:
$path_prefix/lib
(standard library path)$path_prefix/lib/pythonX.Y/site-packages
(third-party library path, where X.Y corresponds to the Python version)- Current working directory (result of the
pwd
command)
The $path_prefix
is derived from the Python interpreter's path, such as /usr
or /usr/local
on Linux.
The $path_prefix
is /Users/USERNAME/.local/lib/pythonX.Y/site-packages
on Mac OSX. ex: /Users/joe/.local/lib/python3.10/site-packages
Essential Functions and Commands:
To navigate and manipulate package locations effectively, familiarize yourself with the following functions and commands:
sys.executable
: Path to the currently used Python interpreter.sys.path
: List of paths searched for the current package.sys.prefix
: Current value of$path_prefix
.- Running
python -m site
in the command line provides information about the current Python environment, including the search path list.
In [2]: sys.path
Out[2]:
['/Users/joe/.pyenv/versions/3.10.5/bin',
'/Users/joe/.pyenv/versions/3.10.5/lib/python310.zip',
'/Users/joe/.pyenv/versions/3.10.5/lib/python3.10',
'/Users/joe/.pyenv/versions/3.10.5/lib/python3.10/lib-dynload',
'',
'/Users/joe/.local/lib/python3.10/site-packages',
'/Users/joe/.pyenv/versions/3.10.5/lib/python3.10/site-packages',
'/Users/joe/Documents/WORK/TOPTAL/DSTAX/ITBatch/src/ositd']
Adding Paths Using Environment Variables:
In instances where a package path is absent from the search path list, add it to the PYTHONPATH
environment variable, separating paths by colons (semicolon on Windows). Exercise caution to prevent the inclusion of paths for different Python versions.
2. Python Library Locations with Virtualenv, Pyenv, and Pipenv
When deploying virtualenv
, it crafts an isolated Python environment with distinct executables in a bin
folder and libraries in a lib
folder. This isolation ensures projects remain insulated from system-wide installations, mitigating potential conflicts.
❯ virtualenv venv
created virtual environment CPython3.11.0.final.0-64 in 221ms
creator CPython3Posix(dest=/Users/joe/Documents/WORK/GLINTECO/PROJECTS/INTERNAL/endeverusfinal/venv, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/joe/Library/Application Support/virtualenv)
added seed packages: pip==23.1.2, setuptools==68.0.0, wheel==0.40.0
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
❯ source venv/bin/activate
❯ which python
/Users/joe/Downloads/venv/bin/python # this is the location of your python
# the location of all libraries will be
❯ ls -al /Users/joe/Downloads/venv/lib/python3.11/site-packages
total 32
drwxr-xr-x 16 joe staff 512 27 Dec 18:11 .
drwxr-xr-x 3 joe staff 96 27 Dec 18:11 ..
drwxr-xr-x 4 joe staff 128 27 Dec 18:11 _distutils_hack
-rw-r--r-- 1 joe staff 18 27 Dec 18:11 _virtualenv.pth
-rw-r--r-- 1 joe staff 5640 27 Dec 18:11 _virtualenv.py
-rw-r--r-- 1 joe staff 151 27 Dec 18:11 distutils-precedence.pth
drwxr-xr-x 8 joe staff 256 27 Dec 18:11 pip
drwxr-xr-x 10 joe staff 320 27 Dec 18:11 pip-23.1.2.dist-info
-rw-r--r-- 1 joe staff 0 27 Dec 18:11 pip-23.1.2.virtualenv
drwxr-xr-x 5 joe staff 160 27 Dec 18:11 pkg_resources
drwxr-xr-x 48 joe staff 1536 27 Dec 18:11 setuptools
drwxr-xr-x 9 joe staff 288 27 Dec 18:11 setuptools-68.0.0.dist-info
-rw-r--r-- 1 joe staff 0 27 Dec 18:11 setuptools-68.0.0.virtualenv
drwxr-xr-x 12 joe staff 384 27 Dec 18:11 wheel
drwxr-xr-x 8 joe staff 256 27 Dec 18:11 wheel-0.40.0.dist-info
-rw-r--r-- 1 joe staff 0 27 Dec 18:11 wheel-0.40.0.virtualenv
For Python version management, pyenv
allocates each installed Python version to its directory within ~/.pyenv/versions/
. The lib
directory contains version-specific library paths. This approach guarantees isolation between Python versions, preventing conflicts and streamlining version switching.
❯ ipython
Python 3.11.0 (main, Nov 28 2022, 10:47:24) [Clang 14.0.0 (clang-1400.0.29.202)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import sys
In [2]: sys.path
Out[2]:
['/Users/joe/.pyenv/versions/3.11.0/envs/resume-genie/bin',
'/Users/joe/.pyenv/versions/3.11.0/lib/python311.zip',
'/Users/joe/.pyenv/versions/3.11.0/lib/python3.11',
'/Users/joe/.pyenv/versions/3.11.0/lib/python3.11/lib-dynload',
'',
'/Users/joe/.pyenv/versions/3.11.0/envs/resume-genie/lib/python3.11/site-packages']
❯ ls -al /Users/joe/.pyenv/versions/3.11.0/lib/python3.11/site-packages
total 16
drwxr-xr-x 10 joe staff 320 28 Nov 2022 .
drwxr-xr-x 208 joe staff 6656 28 Nov 2022 ..
-rw-r--r-- 1 joe staff 119 28 Nov 2022 README.txt
drwxr-xr-x 5 joe staff 160 28 Nov 2022 _distutils_hack
-rw-r--r-- 1 joe staff 151 28 Nov 2022 distutils-precedence.pth
drwxr-xr-x 9 joe staff 288 28 Nov 2022 pip
drwxr-xr-x 10 joe staff 320 28 Nov 2022 pip-22.3.dist-info
drwxr-xr-x 6 joe staff 192 28 Nov 2022 pkg_resources
drwxr-xr-x 48 joe staff 1536 28 Nov 2022 setuptools
drwxr-xr-x 10 joe staff 320 28 Nov 2022 setuptools-65.5.0.dist-info
Depending on what Python version you install, the path will be sth like /Users/joe/.pyenv/versions/3.11.0/lib/python3.11/site-packages
Integrating package management with virtual environments, pipenv
establishes a project-specific virtual environment, storing dependencies in a Pipfile.lock
. This structure guarantees an isolated environment for each project, simplifying dependency management and averting conflicts between projects.
The location is /Users/joe/.local/share/virtualenvs/endeverus-HwqehzDg
in Mac OSX
❯ pipenv --venv
/Users/joe/.local/share/virtualenvs/samples-HwqehzDg
❯ ls -al /Users/joe/.local/share/virtualenvs/samples-HwqehzDg/
total 24
drwxr-xr-x 10 joe staff 320 4 May 2023 .
drwxr-xr-x 4 joe staff 128 27 Dec 17:51 ..
-rw-r--r-- 1 joe staff 40 4 May 2023 .gitignore
-rw-r--r-- 1 joe staff 70 4 May 2023 .project
drwxr-xr-x 37 joe staff 1184 23 May 2023 bin
drwxr-xr-x 3 joe staff 96 4 May 2023 etc
drwxr-xr-x 3 joe staff 96 4 May 2023 lib
-rw-r--r-- 1 joe staff 426 4 May 2023 pyvenv.cfg
drwxr-xr-x 4 joe staff 128 4 May 2023 share
drwxr-xr-x 2 joe staff 64 4 May 2023 src
3. Conclusion
Mastering Python library locations entails comprehending the pivotal role of $path_prefix
and manipulating search paths adeptly. Additionally, employing virtual environments like virtualenv
, pyenv
for version management, and pipenv
for both package and environment management elevates the efficiency of Python development. Armed with this knowledge, developers can troubleshoot issues effectively and gain a profound understanding of Python's package discovery mechanism.
4. FAQs
1. Why do I encounter "executable not found" errors after installing pip?
-
This error often occurs when the path to the Python interpreter is not included in the system's
PATH
variable. Ensure that the directory containing the Python interpreter is added to thePATH
environment variable.
2. How can I troubleshoot "ModuleNotFound" errors when importing modules?
-
Check the
sys.path
variable to ensure that the directory containing the module is included. If not, consider adjusting thePYTHONPATH
environment variable or placing the module in a directory already on the search path.
3. Why does my code work in PyCharm but not in the command prompt?
-
PyCharm may use a specific Python interpreter or virtual environment. Ensure that the command prompt is using the same interpreter or virtual environment. Verify the interpreter path and activate the correct virtual environment if necessary.