Anaconda:永久包含外部包(如在 PYTHONPATH 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37006114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Anaconda: Permanently include external packages (like in PYTHONPATH)
提问by Cord Kaldemeyer
I know how to install packages in Anaconda using conda install
and also how to install packages that are on PyPiwhich is described in the manual.
我知道如何使用 Anaconda 安装软件包,conda install
以及如何安装手册中描述的PyPi上的软件包。
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?
但是如何将包/文件夹永久包含到PYTHONPATH
Anaconda 环境中,以便可以导入我当前正在处理的代码并在重新启动后仍然可用?
My current approach is to use sys
:
我目前的方法是使用sys
:
import sys
sys.path.append(r'/path/to/my/package')
which is not really convenient.
这不是很方便。
Any hints?
任何提示?
Thanks in advance!
提前致谢!
回答by Cord Kaldemeyer
I found two answers to my question in the Anaconda forum:
我在Anaconda 论坛中找到了我的问题的两个答案:
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.
1.) 将模块放入site-packages中,即$HOME/path/to/anaconda/lib/pythonX.X/site-packages
一直在的目录sys.path
。这也应该通过创建符号链接来工作。
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.
2.).pth
在目录中添加一个文件$HOME/path/to/anaconda/lib/pythonX.X/site-packages
。这可以命名为任何名称(它必须以 结尾.pth
)。一个.pth
文件只是一个换行分隔的目录将被添加到在Python启动您的路径的完整路径名的列表。
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/
或者,如果您只想链接到特定的 conda 环境,则将 .pth 文件添加到~/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 developi. e. conda-develop /path/to/module/
to add the module which creates a .pth
file as described under option 2.).
3.) 使用conda developieconda-develop /path/to/module/
添加创建.pth
文件的模块,如选项 2.) 所述。
4.) Create a setup.pyin 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 herefor more information.
4.)在您的包文件夹中创建一个setup.py并使用pip install -e /path/to/package
从我的角度来看最干净的选项进行安装,因为您还可以使用pip list
. 请注意,该选项-e
允许编辑包代码。请参阅此处了解更多信息。
Thanks anyway!
不管怎么说,还是要谢谢你!
回答by Mark Hannel
The preferred solution would be to build your own conda package (information here).
首选的解决方案是构建您自己的 conda 包(此处的信息)。
Another solution would be to create a link between your package directory and any directory in sys.path. In this way, when you ask python to import your package, anaconda will search through its various sys.path directories and it will read the link to your package as if that package were in one of the sys.path directories.
另一种解决方案是在您的包目录和 sys.path 中的任何目录之间创建一个链接。这样,当你要求 python 导入你的包时,anaconda 将搜索它的各种 sys.path 目录,它会读取你的包的链接,就好像该包在 sys.path 目录之一中一样。
Linking a directory can be performed easily with the ln
(link_name) command. As an example:
使用ln
(link_name) 命令可以轻松地链接目录。举个例子:
ln -s /path/to/my/package /path/to/anaconda/lib/python2.7/site-packages/
The above link will allow you to import your package in the default environment of anaconda from any directory. This will not affect any of the other environments.
上面的链接将允许您从任何目录在 anaconda 的默认环境中导入您的包。这不会影响任何其他环境。
If you want to add the package to a specific environment (e.g. - "myenv") within anaconda, you can link the package to one of that particular environment's sys paths:
如果您想将包添加到 anaconda 中的特定环境(例如 - “myenv”),您可以将包链接到该特定环境的 sys 路径之一:
ln -s /path/to/my/package /path/to/anaconda/env/myenv/lib/python2.7/site-packges/
Note the following:
请注意以下事项:
- Linking your package directory to a sys path, rather than actually moving the package directory to a sys path, allows you to keep your package in your directory of choice.
The
-s
flag generates a soft link (much like a shortcut). If you move your package directory, the link will fail to work. Runningln
without the-s
flag generates a hard link (like a mirror copy) that will not be affected by moving (or even deleting..) the package directory. The pros and cons of soft links and hard links are debated hereWindows users should utilize mklink. For information, look here.
回答by Ariel Werle
I'm able to include local modules using the following:
我可以使用以下方法包含本地模块:
conda-develop /path/to/module/
I hope it helps.
我希望它有帮助。
回答by Newskooler
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.
就我这样做,我相信这是最天然的conda
,是通过建立env_vars.sh
在我的环境文件,按照官方文档在这里。
For macOS and Linux users, the steps are as follows:
对于 macOS 和 Linux 用户,步骤如下:
Go to your environment folder (e.g.
/miniconda1/env/env_name
).$CONDA_PREFIX
is the environemnt variable for your environment path.cd $CONDA_PREFIX
Create the
activate.d
anddeactivate.d
directories.mkdir -p ./etc/conda/activate.d mkdir -p ./etc/conda/deactivate.d
Inside the each respective directory, create one
env_vars.sh
file. The one in theactivate.d
directory will set (orexport
) your environment variables when youconda activate
your environment. The file in thedeactivate.d
directory will serve to unset the environment variables when youconda deactivate
your 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.sh
toexport
the 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 tounset
the env variables when youconda deactivate
like so:#!/bin/sh unset VAR_A unset VAR_B
转到您的环境文件夹(例如
/miniconda1/env/env_name
)。$CONDA_PREFIX
是环境路径的环境变量。cd $CONDA_PREFIX
创建
activate.d
和deactivate.d
目录。mkdir -p ./etc/conda/activate.d mkdir -p ./etc/conda/deactivate.d
在每个相应的目录中,创建一个
env_vars.sh
文件。activate.d
目录中的一个将export
在您conda activate
的环境中设置(或)您的环境变量。目录中的deactivate.d
文件将用于在您conda deactivate
的环境中取消设置环境变量。touch ./etc/conda/activate.d/env_vars.sh touch ./etc/conda/deactivate.d/env_vars.sh
首先编辑
$CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
到export
所需的环境变量。#!/bin/sh export VAR_A='some-thing-here' export VAR_B=/path/to/my/file/
然后,打开以编辑
$CONDA_PREFIX/etc/conda/deactivate/env_vars.sh
, 以便unset
在您conda deactivate
喜欢时编辑env 变量:#!/bin/sh unset VAR_A unset VAR_B
Again, the source of my description comes straight from the conda
docs here.
同样,我的描述来源直接来自此处的conda
文档。
回答by Vik
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/
只是添加到上面的 Cord Kaldemeyer 的答案中,作为第二个选项。如果您只想链接到特定的 conda 环境,请将 .pth 文件添加到~/anaconda3/envs/{NAME_OF_ENVIRONMENT}/lib/pythonX.X/site-packages/