Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anaconda: Permanently include external packages (like in PYTHONPATH)

I know how to install packages in Anaconda using conda install and also how to install packages that are on PyPi which is described in the manual.

But how can I permanently include packages/folders into the PYTHONPATH of an Anaconda environment so that code that I am currently working on can be imported and is still available after a reboot?

My current approach is to use sys:

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

which is not really convenient.

Any hints?

Thanks in advance!

like image 343
Cord Kaldemeyer Avatar asked May 03 '16 13:05

Cord Kaldemeyer


People also ask

How do I add conda packages to path?

To permanently include packages or folder into the PYTHONPATH of an Anaconda environment, activate the Conda environment and use [conda develop](https://docs.conda.io/projects/conda-build/en/latest/resources/commands/conda-develop.html) to add the path permanently to the PYTHONPATH of the Conda environment.

Does Anaconda use Pythonpath?

Making Anaconda Python Find Your Python Scripts - Modifying the PYTHONPATH. (The following information applies only to Anaconda installations. The basic idea applies to any python installation, but the precise ways of addressing the issue are different.)

How do I install an external package in Anaconda?

Go to Environments tab just below the Home tab and from there we can check what all packages are installed and what is not. It is very easy to install any package through anaconda navigator, simply search the required package, select package and click on apply to install it.

What packages are included in Anaconda distribution?

The Anaconda distribution comes with packages that can be used on Windows, Linux, and MacOS. The individual edition includes popular package names like numpy , pandas , scipy , sklearn , tensorflow , pytorch , matplotlib , and more.


2 Answers

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

1.) Put the modules into into site-packages, i.e. the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages which is always on sys.path. This should also work by creating a symbolic link.

2.) Add a .pth file to the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages. This can be named anything (it just must end with .pth). A .pth file is just a newline-separated listing of the full path-names of directories that will be added to your path on Python startup.

Alternatively, if you only want to link to a particular conda environment then add the .pth file to ~/anaconda3/envs/{NAME_OF_ENVIRONMENT}/lib/pythonX.X/site-packages/

Both work straightforward and I went for the second option as it is more flexible.

*** UPDATE:

3.) Use conda develop i. e. conda-develop /path/to/module/ to add the module which creates a .pth file as described under option 2.).

4.) Create a setup.py in the folder of your package 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 installations using pip list. Note that the option -e allows to edit the package code. See here for more information.

Thanks anyway!

like image 93
Cord Kaldemeyer Avatar answered Oct 01 '22 14:10

Cord Kaldemeyer


I'm able to include local modules using the following:

conda-develop /path/to/module/ 

I hope it helps.

like image 39
Ariel Werle Avatar answered Oct 01 '22 14:10

Ariel Werle