[英]What is the absolute import and relative import for this kind of directory structure?
I have a directory structure like this,我有一个这样的目录结构,
Assignments作业
├── Assignment1 ├── 作业一
│ ├── Assignment-1.pdf │ ├── 作业-1.pdf
│ └── assignment1.py │ └── assignment1.py
└── Methods └── 方法
├── init .py ├──初始.py
├── primality.py ├── 素数.py
I want to import functions from the primality.py file into the assignment1.py file.我想将函数从primality.py文件导入到assignment1.py文件中。
What is my relative and absolute import line for this dirctory structure?这个目录结构的相对和绝对导入行是什么?
I also tried using this, from..Methods.primality import func
我也试过使用这个,
from..Methods.primality import func
but it gives me an error,但它给了我一个错误,
ImportError: attempted relative import with no known parent package
Can anyone explain me what am I doing wrong here?谁能解释我在这里做错了什么?
You should add the missing __init__.py
as in:您应该添加缺少的
__init__.py
,如下所示:
├── Assignment1
│ ├── Assignment-1.pdf
│ ├── __init__.py # this
│ └── assignment1.py
└── Methods
│ ├── __init__.py
│ ├── primality.py
then go to the Assignments
folder and from there run:然后 go 到
Assignments
文件夹并从那里运行:
$ python3 -m Assignment1.assignment1 # note no py
Then python sees the directory structure as a package structure and Methods/Assignment1 are packages (and you should rename them to methods/assignment1)然后python看到目录结构为package结构,Methods/Assignment1是包(应该重命名为methods/assignment1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.