Python 在 anaconda spyder 中导入自己的 .py 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33409424/
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
Import own .py files in anaconda spyder
提问by user5488652
I've written my own mail.py module in spider (anaconda). I want to import this py file in other python (spider) files just by 'import mail'
我已经用蜘蛛(anaconda)编写了自己的 mail.py 模块。我想通过“导入邮件”将这个 py 文件导入到其他 python (spider) 文件中
I searched on the internet and couldn't find a clearly solution.
我在互联网上搜索,找不到明确的解决方案。
回答by Sahil M
To import any python script, it should exist in the PYTHONPATH. You can check this with the following code:
要导入任何 python 脚本,它应该存在于 PYTHONPATH 中。您可以使用以下代码进行检查:
import sys
print sys.path
To import your Python script:
要导入您的 Python 脚本:
- Put both the scripts (main and the imported python script) in the same directory.
- Add the location of the file to be imported to the sys.path.
- 将两个脚本(主脚本和导入的 python 脚本)放在同一目录中。
- 将要导入的文件的位置添加到 sys.path 中。
For example, if the script is located as '/location/to/file/script.py':
例如,如果脚本位于“/location/to/file/script.py”:
import sys
sys.path.append('/location/to/file/')
import script
回答by faph
There are many options, e.g.
有很多选择,例如
- Place the mail.py file alongside the other python files (this works because the current working dir is on the PYTHONPATH.
- Save a copy of mail.py in the anaconda python environment's "/lib/site-packages" folder so it will be available for any python script using that environment.
- 将 mail.py 文件与其他 python 文件放在一起(这是有效的,因为当前的工作目录在 PYTHONPATH 上。
- 将 mail.py 的副本保存在 anaconda python 环境的“/lib/site-packages”文件夹中,以便它可用于使用该环境的任何 python 脚本。
回答by John Vaughan
I did a slightly different solution approach that is less sophisticated. When I start my anaconda terminal it is at a C prompt. I just did a cd d:\mypython\lib in the beginning window before starting python. once I did that I could simply just import my own classes that I put in that library with "import MyClass as my" then I was off and running. It is interesting, I did 2 days of internet searching in my part time and could not find the answer either, until I asked a friend.
我做了一个稍微不同的解决方法,它不太复杂。当我启动 anaconda 终端时,它处于 C 提示符下。在启动 python 之前,我只是在开始窗口中做了一个 cd d:\mypython\lib 。一旦我这样做了,我就可以简单地导入我自己放入该库中的类,并使用“将 MyClass 作为我的导入”,然后我就可以开始运行了。有趣的是,我在业余时间在网上搜索了 2 天,也找不到答案,直到我问了一个朋友。
cd d:\mypython\lib
python
>>> import MyClass as my
>>> my1=my.MyClass()
>>> my1.doSomething()
worked for me on my anaconda / windows 10 environment python 3.6.6.
在我的 anaconda/windows 10 环境 python 3.6.6 上为我工作。
回答by Bamwani
I had the same problem, my files were in same folder, yet it was throwing an error while importing the "to_be_imported_file.py".
我遇到了同样的问题,我的文件在同一个文件夹中,但在导入“to_be_imported_file.py”时抛出错误。
I had to run the "to_be_imported_file.py" seperately before importing it to another file.
在将“to_be_imported_file.py”导入另一个文件之前,我必须单独运行它。
I hope it works for you too.
我希望它也适用于你。