[英]ImportError: attempted relative import with no known parent package
Can't seem to find a straightforward answer to this question. 似乎无法找到这个问题的直截了当的答案。 All I want to know is how to import the file I saved under my
\\scripts
directory. 我想知道的是如何导入我保存在
\\scripts
目录下的文件。
I added the path to sys.path
... 我添加了
sys.path
的路径...
import sys
sys.path.insert(0, "C:\\my_Stuff\\data_science\\scripts")
I added a __init__.py
file under my \\scripts
directory, which is the same directory where my tree.py
file is. 我在
\\scripts
目录下添加了一个__init__.py
文件,该目录与我的tree.py
文件所在的目录相同。
I run... 我跑...
import tree as tr
...but am still getting the error. ...但我仍然得到错误。
My answer is probably late but the following is working for me. 我的回答可能很晚,但以下内容对我有用。
Let's say i have " my_file.py " in the directory " folder ": 假设我在目录“ folder ”中有“ my_file.py ”:
import sys
sys.path.append('path\\to\\folder')
from my_file import ...
Hope this can help! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.