python 相对路径导入_从Python中的相对路径导入
I have a folder for my client code, a folder for my server code, and a folder for code that is shared between them
Proj/
Client/
Client.py
Server/
Server.py
Common/
__init__.py
Common.py
How do I import Common.py from Server.py and Client.py?
解决方案
Don't do relative import.
From PEP8:
Relative imports for intra-package imports are highly discouraged.
Put all your code into one super package (i.e. "myapp") and use subpackages for client, server and common code.
Update:
"Python 2.6 and 3.x supports proper relative imports (...)". See Dave's answers for more details.