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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:40:49  来源:igfitidea点击:

Anaconda: Permanently include external packages (like in PYTHONPATH)

pythonanaconda

提问by Cord Kaldemeyer

I know how to install packages in Anaconda using conda installand 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 PYTHONPATHof an Anaconda environment so that code that I am currently working on can be imported and is still available after a reboot?

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

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-packageswhich 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 .pthfile to the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages. This can be named anything (it just must end with .pth). A .pthfile 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 .pthfile 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/packagewhich is the cleanest option from my point of view because you can also see all installations using pip list. Note that the option -eallows 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 -sflag generates a soft link (much like a shortcut). If you move your package directory, the link will fail to work. Running lnwithout the -sflag 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 here

  • Windows users should utilize mklink. For information, look here.

  • 将包目录链接到 sys 路径,而不是实际将包目录移动到 sys 路径,允许您将包保留在您选择的目录中。
  • -s标志生成一个软链接(很像快捷方式)。如果您移动包目录,链接将无法工作。在ln没有-s标志的情况下运行会生成一个硬链接(如镜像副本),该链接不会受到移动(甚至删除..)包目录的影响。软链接和硬链接的优缺点在这里辩论

  • Windows 用户应该使用 mklink。有关信息,请查看此处

回答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.shfiles 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 用户,步骤如下:

  1. Go to your environment folder (e.g. /miniconda1/env/env_name). $CONDA_PREFIXis the environemnt variable for your environment path.

    cd $CONDA_PREFIX
    
  2. Create the activate.dand deactivate.ddirectories.

    mkdir -p ./etc/conda/activate.d
    mkdir -p ./etc/conda/deactivate.d
    
  3. Inside the each respective directory, create one env_vars.shfile. The one in the activate.ddirectory will set (or export) your environment variables when you conda activateyour environment. The file in the deactivate.ddirectory will serve to unset the environment variables when you conda deactivateyour environment.

    touch ./etc/conda/activate.d/env_vars.sh
    touch ./etc/conda/deactivate.d/env_vars.sh
    
  4. First edit the $CONDA_PREFIX/etc/conda/activate.d/env_vars.shto exportthe desired environment variables.

    #!/bin/sh
    
    export VAR_A='some-thing-here'
    export VAR_B=/path/to/my/file/
    
  5. Afterwards, open to edit the $CONDA_PREFIX/etc/conda/deactivate/env_vars.sh, in order to unsetthe env variables when you conda deactivatelike so:

    #!/bin/sh
    
    unset VAR_A
    unset VAR_B
    
  1. 转到您的环境文件夹(例如/miniconda1/env/env_name)。$CONDA_PREFIX是环境路径的环境变量。

    cd $CONDA_PREFIX
    
  2. 创建activate.ddeactivate.d目录。

    mkdir -p ./etc/conda/activate.d
    mkdir -p ./etc/conda/deactivate.d
    
  3. 在每个相应的目录中,创建一个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
    
  4. 首先编辑$CONDA_PREFIX/etc/conda/activate.d/env_vars.shexport所需的环境变量。

    #!/bin/sh
    
    export VAR_A='some-thing-here'
    export VAR_B=/path/to/my/file/
    
  5. 然后,打开以编辑$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 condadocs 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/