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!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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!
Method 2
I’m able to include local modules using the following:
conda-develop /path/to/module/
I hope it helps.
Method 3
The way I do this, which I believe is the most native to conda, is by creating env_vars.sh files in my environment, as per the official documentation here.
For macOS and Linux users, the steps are as follows:
-
Go to your environment folder (e.g.
/miniconda1/env/env_name).$CONDA_PREFIXis the environemnt variable for your environment path.cd $CONDA_PREFIX
-
Create the
activate.danddeactivate.ddirectories.mkdir -p ./etc/conda/activate.d mkdir -p ./etc/conda/deactivate.d
-
Inside the each respective directory, create one
env_vars.shfile. The one in theactivate.ddirectory will set (orexport) your environment variables when youconda activateyour environment. The file in thedeactivate.ddirectory will serve to unset the environment variables when youconda deactivateyour environment.touch ./etc/conda/activate.d/env_vars.sh touch ./etc/conda/deactivate.d/env_vars.sh
-
First edit the
$CONDA_PREFIX/etc/conda/activate.d/env_vars.shtoexportthe desired environment variables.#!/bin/sh export VAR_A='some-thing-here' export VAR_B=/path/to/my/file/
-
Afterwards, open to edit the
$CONDA_PREFIX/etc/conda/deactivate/env_vars.sh, in order tounsetthe env variables when youconda deactivatelike so:#!/bin/sh unset VAR_A unset VAR_B
Again, the source of my description comes straight from the conda docs here.
Method 4
Just to add to Cord Kaldemeyer’s answer above, for the 2nd option. 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/
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0