ChatGPT解决这个技术问题 Extra ChatGPT

Where is site-packages located in a Conda environment?

After installing a package in an Conda environment, I'd like to make some changes to the code in that package.

Where can I find the site-packages directory containing the installed packages?

I have an Anaconda Python 2.7 base distribution, but I do not find a directory:

/Users/username/anaconda/lib/python2.7/site-packages
Important Note: Conda tracks file sizes to detect corruption/package clobbering, and since some files are hardlinked, editing in one environment could lead to corruption in all environments and the package cache. If one plans to edit files manually for a particular package, consider first reinstalling the package in copy-only mode with a conda install --copy --force-reinstall <pkg_name> operation.

A
Arcturus B

You can import the module and check the module.__file__ string. It contains the path to the associated source file.

Alternatively, you can read the File tag in the the module documentation, which can be accessed using help(module), or module? in IPython.


My problem is that I get a package import "not found" error for the package that conda supposedly installed. So I would like to check where conda thinks that it installed the package.
@BenFarmer: this sounds like a rather different problem. Have you tried searching for something along the line of "conda list installed package paths"? This returns stackoverflow.com/q/46767012 and stackoverflow.com/q/47138241.
thanks @Arcturus B just adding one example example: >>import tensorflow >>tensorflow.__file__
my problem is that I see it in conda list but I can't find where it would be located...any ideas?
V
Vlad Costin

Run this inside python shell:

from distutils.sysconfig import get_python_lib
print(get_python_lib())

my problem is that I see it in conda list but I can't find where it would be located...any ideas?
S
Steve

Linux users can find the locations of all the installed packages like this:

pip list | xargs -exec pip show

Updated 2022-03-21 to remove the unwanted table heading at the top of pip list output:

pip list | tail -n +3 | xargs -exec pip show

Here is some elegant use of xargs!
i just used: pip show . It worked, I got the files.
but he said he is using conda...?
my problem is that I see it in conda list but I can't find where it would be located...any ideas?
@CharlieParker it sounds like you want to be able to list the files in a given conda package: stackoverflow.com/questions/47138241/…. Verify that the package is installed in the correct environment (the one your Python binary is part of), and that it includes files in directories that Python looks at (list those paths with python -m site).
N
Nick Veld

One more option using the interpreter:

import site; print(''.join(site.getsitepackages()))

And using a terminal/prompt:

python -c "import site; print(''.join(site.getsitepackages()))"

Also in this case you can easily print one of the directory (in case there are more than one) using own filter


H
Hezi Zhang

I installed miniconda and found all the installed packages in /miniconda3/pkgs


The Conda package cache is not the same as the Python's site-packages. You shouldn't mess with these, otherwise you'll could end up with corruption complaints from Conda.
j
jeff_carter

You could also type 'conda list' in a command line. This will print out the installed modules with the version numbers. The path within your file structure will be printed at the top of this list.


conda list, just gave me " packages in environment at /Users/user/anaconda:" as output. The exact location for the packages was required, which is /Users/user/anaconda/lib/python2.7/'.
'conda list' is a very good idea when you want to know from which repository a package was installed. It also shows an installation via pip. So this is a really good tip although it does not answer the question.
This answer actually works for me - not sure why it's downvoted! conda list, as its first line, spits out the root of your conda installation; inside that is the pkgs folder.
... actually (re. my preceding comment), inside the root of the miniconda installation I find some packages at 'lib/python3.7/site-packages'
my problem is that I see it in conda list but I can't find where it would be located...any ideas?
R
Rafay Khan

The location should be (in Linux systems):

home/<USERNAME>/anaconda3/envs/<ENV_NAME>/lib/python<VERSION>/site-packages/

B
BHA Bilel

You should find installed packages in :

anaconda's directory / lib / site_packages

That's where i found mine.


J
James Perez

At least with Miniconda (I assume it's the same for Anaconda), within the environment folder, the packages are installed in a folder called \conda-meta.

i.e.

C:\Users\username\Miniconda3\envs\environmentname\conda-meta

If you install on the base environment, the location is:

C:\Users\username\Miniconda3\pkgs


That is the package cache, which is not the same as site-packages for the Python installation.
m
merv

Generic approach for environment foo from Conda CLI would be:

conda run -n foo python -m site

which for my base environment looks like:

$ conda run -n base python -m site

sys.path = [
    '/Users/mfansler/miniconda3/lib/python3.8',
    '/Users/mfansler/miniconda3/lib/python38.zip',
    '/Users/mfansler/miniconda3/lib/python3.8/lib-dynload',
    '/Users/mfansler/miniconda3/lib/python3.8/site-packages',
]
USER_BASE: '/Users/mfansler/.local' (exists)
USER_SITE: '/Users/mfansler/.local/lib/python3.8/site-packages' (doesn't exist)
ENABLE_USER_SITE: True

The answer is the site-packages in the sys.path list.


H
Huanfa Chen

I encountered this issue in my conda environment. The reason is that packages have been installed into two different folders, only one of which is recognised by the Python executable.

~/anaconda2/envs/[my_env]/site-packages ~/anaconda2/envs/[my_env]/lib/python2.7/site-packages

A proved solution is to add both folders to python path, using the following steps in command line (Please replace [my_env] with your own environment):

conda activate [my_env]. conda-develop ~/anaconda2/envs/[my_env]/site-packages conda-develop ~/anaconda2/envs/[my_env]/lib/python2.7/site-packages (conda-develop is to add a .pth file to the folder so that the Python executable knows of this folder when searching for packages.)

To ensure this works, try to activate Python in this environment, and import the package that was not found.