简体   繁体   English

如何将python脚本添加到Anaconda库?

[英]How do I add my python scripts to Anaconda library?

I have a python script foo.py that I wrote. 我有一个编写的python脚本foo.py In which directory can I put that script so that I can use from foo import *** without getting any error. 我可以将该脚本放在哪个目录中,以便可以from foo import ***而不会出现任何错误。 I am using Windows system with anaconda. 我将Windows系统与anaconda一起使用。

In case of conda environments on windows you can simply check where the external packages for conda (or that conda environment) are installed. 如果在Windows上使用conda环境,则只需检查一下conda外部软件包(或该conda环境)的安装位置即可。

For example type something like this into the Python prompt of your conda environment: 例如,在您的conda环境的Python提示符中键入以下内容:

>>> import numpy   # or any other external package that you installed with conda
>>> numpy.__file__
'C:\\Anaconda\\envs\\work\\lib\\site-packages\\numpy\\__init__.py'

That gives you the path where you're normal packages would be installed (well, minus the \\\\numpy\\\\__init__.py part). 这为您提供了普通软件包的安装路径(好吧,减去\\\\numpy\\\\__init__.py部分)。 So if I put a file in that directory, in my case C:\\\\Anaconda\\\\envs\\\\work\\\\lib\\\\site-packages\\\\ then I can load it. 因此,如果我将文件放在该目录中,以我的情况为C:\\\\Anaconda\\\\envs\\\\work\\\\lib\\\\site-packages\\\\则可以加载它。

Just make sure that you really import a package that has been installed with conda install in that environment! 只要确保您确实导入了在该环境中使用conda install的软件包即可!

For example: 例如:

C:\\Anaconda\\envs\\work\\lib\\site-packages\\fun.py C:\\ Anaconda \\ envs \\ work \\ lib \\ site-packages \\ fun.py

def c():
    return 10

Python prompt (again) Python提示(再次)

>>> from fun import c
>>> c()
10

That will make the file accessible for the corresponding conda environment (and just there). 这将使该文件可用于相应的conda环境(并在那里)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM