英文:
How to import a file from another folder in a package
问题
My project structure looks like this
pythonProject
|
|-__init__.py
|
|- a
| |- file_a.py
| |-__init__.py
|
|- b
|- file_b.py
|-__init__.py
I've also attached the image of my project's structure below.
My file_a.py
looks like:
class class_a:
def __init__(self):
pass
def show(self):
print('class A')
if __name__ == "__main__":
pass
My file_b.py
looks like:
from pythonProject.a.file_a import class_a
class class_b:
def __init__(self):
pass
def show(self):
a = class_a()
a.show()
if __name__ == "__main__":
b = class_b()
But I keep getting an error No module named 'pythonProject'
. All the folders do have __init__.py
file. What am I doing wrong?
On the terminal, I run the command python b/file_b.py
from inside the pythonProject.
Also, I would not like to add project path using sys library.
Thanks.
Things I've tried.
- I added all the
__init__.py
file in all the folders. But all of them are empty. Should they have any specific references? - I tried the import line to
..a.file_a import class_a
. Then I get the error:ImportError: attempted relative import with no known parent package
- I tried the import line to
a.file_a import class_a
but then the error isModuleNotFoundError: No module named 'a'
.
英文:
My project structure looks like this
pythonProject
|
|-__init__.py
|
|- a
| |- file_a.py
| |-__init__.py
|
|- b
|- file_b.py
|-__init__.py
I've also attached the image of my project's structure below.
My file_a.py
looks like:
class class_a:
def __init__(self):
pass
def show(self):
print('class A')
if __name__ == "__main__":
pass
My file_b.py
looks like:
from pythonProject.a.file_a import class_a
class class_b:
def __init__(self):
pass
def show(self):
a = class_a()
a.show()
if __name__ == "__main__":
b = class_b()
But I keep getting an error No module named 'pythonProject'
. All the folders do have __init__.py
file. What am I doing wrong?
On the terminal, I run the command python b/file_b.py
from inside the pythonProject.
Also, I would not like to add project path using sys library.
Thanks.
Things I've tried.
- I added all the
__init__.py
file in all the folders. But all of them are empty. Should they have any specific references? - I tried the import line to
..a.file_a import class_a
. Then I get the error:ImportError: attempted relative import with no known parent package
- I tried the import line to
a.file_a import class_a
but then the error isModuleNotFoundError: No module named 'a'
.
答案1
得分: 1
以下是翻译好的部分:
我认为问题似乎出现在你的a.py
和b.py
文件中的if __name__ == "main"
条件上。正确的字符串应该是"__main__"
(两侧都有双下划线)
在两个文件中将条件更改为if __name__ == "__main__":
,然后尝试再次运行b.py
文件。这应该解决错误
> No module named 'pythonProject'.
在导入方面,你可以在b.py
文件中使用绝对导入语法从a.py
文件中导入a
类
from pythonProject.a.file_a import class_a
只要你在pythonProject
目录内运行b.py
文件,这应该可以工作。
英文:
I think the problem appears to be with your if __name__ == "main"
condition in files a.py
and b.py
. Instead of "main",
the correct string is "__main__"
(double underscores on each side)
Change the condition in both files to if __name__ == "__main__":
and try running file b.py
again. This should resolve the error
> No module named 'pythonProject'.
In terms of imports, you can import class a from file a.py
using the absolute import syntax in file b.py
from pythonProject.a.file_a import class_a
As long as you run file b.py
from within the pythonProject
directory, this should work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论