Anaconda:永久包含外部包(如在 PYTHONPATH 中)

时间:2023-03-02
本文介绍了Anaconda:永久包含外部包(如在 PYTHONPATH 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用 conda install 在 Anaconda 中安装软件包,以及如何安装 PyPi 在 manual 中有描述.p>

但是如何将包/文件夹永久包含在 Anaconda 环境的 PYTHONPATH 中,以便可以导入我当前正在处理的代码并且在重新启动后仍然可用?

我目前的做法是使用sys:

导入系统sys.path.append(r'/path/to/my/package')

这不是很方便.

有什么提示吗?

提前致谢!

解决方案

我在 Anaconda 论坛:

1.) 将模块放入站点包中,即 $HOME/path/to/anaconda/lib/pythonX.X/site-packages 目录,它总是在 上sys.path.这也应该通过创建符号链接来工作.

2.) 将 .pth 文件添加到目录 $HOME/path/to/anaconda/lib/pythonX.X/site-packages.这可以命名为任何名称(它必须以 .pth 结尾)..pth 文件只是一个以换行符分隔的目录的完整路径名列表,这些目录将在 Python 启动时添加到您的路径中.

或者,如果您只想链接到特定的 conda 环境,请将 .pth 文件添加到 ~/anaconda3/envs/{NAME_OF_ENVIRONMENT}/lib/pythonX.X/site-packages/

两者都很简单,我选择了第二个选项,因为它更灵活.

***更新:

3.) 使用 conda develop 一世.e.conda-develop/path/to/module/ 添加创建 .pth 文件的模块,如选项 2 所述.).

4.) 在包的文件夹中创建一个 setup.py并使用 pip install -e/path/to/package 安装它,从我的角度来看,这是最干净的选项,因为您还可以使用 pip list 查看所有安装.请注意,选项 -e 允许编辑包代码.请参阅此处了解更多信息.

还是谢谢!

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!

解决方案

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!

这篇关于Anaconda:永久包含外部包(如在 PYTHONPATH 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:从 cronjob 运行 bash 脚本失败,并显示“没有这样 下一篇:Conda 仅为 env 设置 LD_LIBRARY_PATH

相关文章

最新文章