如何从包中的另一个文件夹导入文件

huangapple go评论104阅读模式
英文:

How to import a file from another folder in a package

问题

My project structure looks like this

  1. pythonProject
  2. |
  3. |-__init__.py
  4. |
  5. |- a
  6. | |- file_a.py
  7. | |-__init__.py
  8. |
  9. |- b
  10. |- file_b.py
  11. |-__init__.py

I've also attached the image of my project's structure below.

enter image description here

My file_a.py looks like:

  1. class class_a:
  2. def __init__(self):
  3. pass
  4. def show(self):
  5. print('class A')
  6. if __name__ == "__main__":
  7. pass

My file_b.py looks like:

  1. from pythonProject.a.file_a import class_a
  2. class class_b:
  3. def __init__(self):
  4. pass
  5. def show(self):
  6. a = class_a()
  7. a.show()
  8. if __name__ == "__main__":
  9. 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.

  1. I added all the __init__.py file in all the folders. But all of them are empty. Should they have any specific references?
  2. 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
  3. I tried the import line to a.file_a import class_a but then the error is ModuleNotFoundError: No module named 'a'.
英文:

My project structure looks like this

  1. pythonProject
  2. |
  3. |-__init__.py
  4. |
  5. |- a
  6. | |- file_a.py
  7. | |-__init__.py
  8. |
  9. |- b
  10. |- file_b.py
  11. |-__init__.py

I've also attached the image of my project's structure below.

enter image description here

My file_a.py looks like:

  1. class class_a:
  2. def __init__(self):
  3. pass
  4. def show(self):
  5. print('class A')
  6. if __name__ == "__main__":
  7. pass

My file_b.py looks like:

  1. from pythonProject.a.file_a import class_a
  2. class class_b:
  3. def __init__(self):
  4. pass
  5. def show(self):
  6. a = class_a()
  7. a.show()
  8. if __name__ == "__main__":
  9. 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.

  1. I added all the __init__.py file in all the folders. But all of them are empty. Should they have any specific references?
  2. 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
  3. I tried the import line to a.file_a import class_a but then the error is ModuleNotFoundError: No module named 'a'.

答案1

得分: 1

以下是翻译好的部分:

我认为问题似乎出现在你的a.pyb.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.

huangapple
  • 本文由 发表于 2023年3月1日 08:15:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定