Anaconda: constantly include external packages (e.g. in PYTHONPATH)

I know how to install packages in Anaconda using conda install , as well as how to create packages that are on PyPi , which is described in the manual .

But how can I constantly include packages / folders in Anaconda, for example. with the code I'm working on now?

My current approach is to use sys :

 import sys sys.path.append(r'/path/to/my/package') 

which is not very convenient.

Any clues?

Thanks in advance!

+7
python anaconda
source share
2 answers

I found two answers to my question in the Anaconda forum :

1.) Put the modules in site packages, that is, the $HOME/path/to/anaconda/lib/pythonX.X/site-packages , which is always on sys.path . This should also work by creating a symlink.

2.) Add the .pth file to the $HOME/path/to/anaconda/lib/pythonX.X/site-packages . This can be called anything (it should end in .pth ). A .pth file is just a listing with a full list of directory names that will be added to your path when you start Python.

Both work simply, and I went for the second option, since it is more flexible.

*** UPDATE:

3.) Create setup.py in your package folder and install it using pip install -e /path/to/package , which is the cleanest option from my point of view, because you can also see all the installations using pip list .

Thanks anyway!

+10
source share

The preferred solution would be to create your own conda package (info here ).

Another solution would be to create a link between your package directory and any directory in sys.path. That way, when you ask python to import your package, anaconda will look in its various sys.path directories and will read the link to your package as if that package were in one of the sys.path directories.

Directory binding can be easily done using the ln (link_name) command. As an example:

 ln -s /path/to/my/package /path/to/anaconda/lib/python2.7/site-packages/ 

The above link will allow you to import your package into the default anaconda environment from any directory. This will not affect any of the other environments.

If you want to add a package to a specific environment (for example, "myenv") in anaconda, you can associate the package with one of these specific sys paths of the environment:

 ln -s /path/to/my/package /path/to/anaconda/env/myenv/lib/python2.7/site-packges/ 

Please note the following:

  • Linking your package directory to the sys directory, rather than moving the package directory to the sys path, allows you to save your package in the directory of your choice.
  • The -s flag generates a soft link (very similar to a shortcut). If you move the directory of your package, the link will not work. Running ln without the -s flag creates a hard link (for example, a mirrored copy) that will not be affected by moving (or even deleting) the package directory. All the pros and cons of soft links and hard links are discussed here.

  • Windows users must use mklink. For information see here .

+2
source share

All Articles