Anaconda:永久包含外部软件包(例如PYTHONPATH中的软件包) [英] Anaconda: Permanently include external packages (like in PYTHONPATH)
问题描述
我知道如何使用conda install
在Anaconda中安装软件包,以及如何安装 PyPi 上的软件包手册中所述.
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.
但是如何将软件包/文件夹永久包含在Anaconda环境的PYTHONPATH
中,以便可以导入我当前正在使用的代码,并且在重新启动后仍然可用?
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?
我当前的方法是使用sys
:
import sys
sys.path.append(r'/path/to/my/package')
这不是很方便.
有任何提示吗?
提前谢谢!
推荐答案
我在 1.)将模块放入站点软件包中,即始终位于sys.path
的目录$HOME/path/to/anaconda/lib/pythonX.X/site-packages
中.通过创建符号链接,这也应该起作用.
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.)将.pth
文件添加到目录$HOME/path/to/anaconda/lib/pythonX.X/site-packages
.可以将其命名为任何名称(仅必须以.pth
结尾). .pth
文件只是用换行符分隔的目录完整路径名列表,该目录将在Python启动时添加到您的路径中.
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.
或者,如果您只想链接到特定的conda环境,则将.pth文件添加到~/anaconda3/envs/{NAME_OF_ENVIRONMENT}/lib/pythonX.X/site-packages/
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.
***更新:
3.)使用 conda开发 i. e. conda-develop /path/to/module/
添加按选项2)中所述创建.pth
文件的模块.
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.)在包的文件夹中创建 setup.py 并使用pip install -e /path/to/package
进行安装,从我的角度来看,这是最干净的选择,因为您还可以使用pip list
查看所有安装.请注意,选项-e
允许编辑程序包代码.有关更多信息,请参见此处.
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.
还是谢谢!
这篇关于Anaconda:永久包含外部软件包(例如PYTHONPATH中的软件包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!