我有一个存储所有.py文件的目录.
bin/
main.py
user.py # where class User resides
dir.py # where class Dir resides
Run Code Online (Sandbox Code Playgroud)
我想在main.py中使用user.py和dir.py中的类.
如何将这些Python类导入main.py?
此外,User如果user.py位于子目录中,如何导入类?
bin/
dir.py
main.py
usr/
user.py
Run Code Online (Sandbox Code Playgroud) 我可能遗漏了一些显而易见的东西但无论如何
当您os在python中导入包时,您可以使用任何子模块/子包.例如,这有效:
>>> import os
>>> os.path.abspath(...)
Run Code Online (Sandbox Code Playgroud)
但是我有自己的包,其结构如下:
FooPackage/
__init__.py
foo.py
Run Code Online (Sandbox Code Playgroud)
这里相同的逻辑不起作用:
>>> import FooPackage
>>> FooPackage.foo
AttributeError: 'module' object has no attribute 'foo'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?