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

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

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.

enter image description here

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.

  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

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.

enter image description here

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.

  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:

确定