問題描述
我知道如何使用 conda install
在 Anaconda 中安裝軟件包,以及如何安裝 PyPi 在 manual 中有描述.p>
但是如何將包/文件夾永久包含在 Anaconda 環(huán)境的 PYTHONPATH
中,以便可以導(dǎo)入我當(dāng)前正在處理的代碼并且在重新啟動(dòng)后仍然可用?
我目前的做法是使用sys
:
導(dǎo)入系統(tǒng)sys.path.append(r'/path/to/my/package')
這不是很方便.
有什么提示嗎?
提前致謝!
我在 Anaconda 論壇:
1.) 將模塊放入站點(diǎn)包中,即 $HOME/path/to/anaconda/lib/pythonX.X/site-packages
目錄,它總是在 上sys.path
.這也應(yīng)該通過創(chuàng)建符號(hào)鏈接來工作.
2.) 將 .pth
文件添加到目錄 $HOME/path/to/anaconda/lib/pythonX.X/site-packages
.這可以命名為任何名稱(它必須以 .pth
結(jié)尾)..pth
文件只是一個(gè)以換行符分隔的目錄的完整路徑名列表,這些目錄將在 Python 啟動(dòng)時(shí)添加到您的路徑中.
或者,如果您只想鏈接到特定的 conda 環(huán)境,請(qǐng)將 .pth 文件添加到 ~/anaconda3/envs/{NAME_OF_ENVIRONMENT}/lib/pythonX.X/site-packages/
兩者都很簡(jiǎn)單,我選擇了第二個(gè)選項(xiàng),因?yàn)樗`活.
***更新:
3.) 使用 conda develop 一世.e.conda-develop/path/to/module/
添加創(chuàng)建 .pth
文件的模塊,如選項(xiàng) 2 所述.).
4.) 在包的文件夾中創(chuàng)建一個(gè) setup.py并使用 pip install -e/path/to/package
安裝它,從我的角度來看,這是最干凈的選項(xiàng),因?yàn)槟€可以使用 pip list
查看所有安裝.請(qǐng)注意,選項(xiàng) -e
允許編輯包代碼.請(qǐng)參閱此處了解更多信息.
還是謝謝!
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!
這篇關(guān)于Anaconda:永久包含外部包(如在 PYTHONPATH 中)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!