我有一个用于客户端代码的文件夹,一个用于服务器端代码的文件夹,以及一个用于在它们之间共享的代码的文件夹
Proj/
Client/
Client.py
Server/
Server.py
Common/
__init__.py
Common.py
如何从Server.py和Client.py中导入Common.py?
我有一个用于客户端代码的文件夹,一个用于服务器端代码的文件夹,以及一个用于在它们之间共享的代码的文件夹
Proj/
Client/
Client.py
Server/
Server.py
Common/
__init__.py
Common.py
如何从Server.py和Client.py中导入Common.py?
Python 2.6和3.x支持正确的相对导入,您可以避免使用任何hacky方法。 使用此方法,您知道您正在获取一个相对导入而不是绝对导入。 '..' 的意思是,进入上一级目录:
from ..Common import Common
需要注意的是,这个方法仅在将Python作为模块从包外部运行时才有效。例如:
python -m Proj
在某些情况下,仍然会普遍使用这种方法,其中您实际上从未“安装”过软件包。例如,它在Django用户中很受欢迎。
您可以将Common/添加到sys.path(Python查找要导入的路径列表)中:
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'Common'))
import Common
os.path.dirname(__file__)
只会返回当前 Python 文件所在的目录,然后我们进入 'Common/' 目录并导入 'Common' 模块。
from ...myfile
变成 ../../myfile
。 - WattsInABox../../mydir2/myfile
? - Aaron John Sabu有趣的是,我遇到了同样的问题,并通过以下方式解决:
结合Linux命令ln
,我们可以使事情变得更简单:
1. cd Proj/Client
2. ln -s ../Common ./
3. cd Proj/Server
4. ln -s ../Common ./
如果你想从文件中导入Proj/Common/Common.py
的some_stuff
到你的文件Proj/Client/Client.py
,可以像这样:
# in Proj/Client/Client.py
from Common.Common import some_stuff
对于Proj/Server
,同样适用,对于setup.py
流程也是如此,
这里讨论了同样的问题,希望能有所帮助!
进行相对导入完全没问题!这是我这个小家伙的做法:
#first change the cwd to the script path
scriptPath = os.path.realpath(os.path.dirname(sys.argv[0]))
os.chdir(scriptPath)
#append the relative location you want to import from
sys.path.append("../common")
#import your module stored in '../common'
import common.py
默认的导入方法已经是“相对”的,来自PYTHONPATH。 PYTHONPATH默认情况下包括一些系统库以及源文件所在文件夹。如果你使用-m运行模块,则当前目录会被添加到PYTHONPATH中。因此,如果您的程序入口点位于Proj内部,则在Server.py和Client.py中使用import Common.Common
应该都可以正常工作。
不要使用相对导入,它不会按照您想要的方式工作。
假设我们在当前工作目录中运行ls -R
,这是结果:
./second_karma:
enemy.py import.py __init__.py math
./second_karma/math:
fibonacci.py __init__.py
我们运行这个命令:$ python3 second-karma/import.py
init.py是一个空文件,但它应该存在。
现在让我们看看second-karma/import.py
里面有什么:
from .math.fibonacci import Fibonacci
fib = Fibonacci()
print(fib.get_fibonacci(15))
第二个文件 second_karma/math/fibonacci.py
中包含什么内容:
from ..enemy import Enemy
class Fibonacci:
enemy: Enemy
def __init__(self):
self.enemy = Enemy(150,900)
print("Class instantiated")
def get_fibonacci(self, which_index: int) -> int:
print(self.enemy.get_hp())
return 4
second_karma/enemy.py
:class Enemy:
hp: int = 100
attack_low: int = 180
attack_high: int = 360
def __init__(
self,
attack_low: int,
attack_high: int) -> None:
self.attack_low = attack_low
self.attack_high = attack_high
def getAttackPower(
self) -> {"attack_low": int, "attack_high": int}:
return {
"attack_low": self.attack_low,
"attack_high": self.attack_high
}
def get_hp(self) -> int:
return self.hp
python3 path/to/file.py
).import.py
makes reference to importing .math
.math
in this context means "go find a module/package in the current package with the name math"$ python3 second-karma/import.py
I am executing a module, not a package. thus python has no idea what .
means in this contextpython3 -m second_karma.import
import.py
is of parent package second_karma
, and thus your relative import will work.这些 __init__.py
是必需的,如果没有它们,您必须先创建它们。
README.md
以更好地理解import os, sys
script_path = os.path.realpath(os.path.dirname(__name__))
os.chdir(script_path)
sys.path.append("..")
# above mentioned steps will make 1 level up module available for import
# here Client, Server and Common all 3 can be imported.
# below mentioned import will be relative to root project
from Common import Common
from Client import Client