简体   繁体   English

python模块和导入

[英]python module and imports

If this is my directory tree 如果这是我的目录树

temp
├── __init__.py
└── __main__.py

0 directories, 2 files

And I have the following code in __init__.py and in __main__.py 我在__init__.py__main__.py有以下代码

__init__.py __init__.py

"""Initializes the module"""

CONSTANT = 1
sys.exit("what is happening here")

__main__.py __main__.py

# from . import CONSTANT
# from temp import CONSTANT
if __name__ == "__main__":

    print "This should never run"

I am getting two problems here that I am trying to figure out 我在这里遇到两个问题,我想弄明白

On running python . 在运行python . in the temp directory I get the output This should never run , shouldn't the module be initialized first with the __init__.py file resulting in the abort? temp目录中我得到输出This should never run ,不应该先用__init__.py文件初始化模块导致中止?

Second how do I go about doing imports in python modules? 第二,我如何在python模块中进行导入? Neither of the two options I have mentioned above works. 我上面提到的两个选项都没有用。 I can neither do from . import CONSTANT 我做不到from . import CONSTANT from . import CONSTANT nor from temp import CONSTANT in the code above. 在上面的代码中from . import CONSTANTfrom temp import CONSTANT What is the right way to do relative imports? 做相对进口的正确方法是什么?

I am running this on Python 2.7.5, apologies if this has already been asked before. 我在Python 2.7.5上运行它,如果之前已经问过这个,请道歉。

You should be running it from out of the temp directory. 您应该从temp目录中运行它。 If someDir contains your temp directory, then: 如果someDir包含您的temp目录,则:

someDir $ python -m temp   #someDir/temp/__init__.py is your file.

On running python . 在运行python。 in the temp directory I get the output This should never run, shouldn't the module be initialized first with the init .py file resulting in the abort? 在临时目录中我得到输出这应该永远不会运行,不应该先使用init .py文件初始化模块导致中止?

If you run it from outside, __init__.py will be called. 如果从外部运行它,将调用__init__.py And sys.exit will be called too. 并且还将调用sys.exit


Second how do I go about doing imports in python modules? 第二,我如何在python模块中进行导入? Neither of the two options I have mentioned above works. 我上面提到的两个选项都没有用。 I can neither do from . 我做不到。 import CONSTANT nor from temp import CONSTANT in the code above. 在上面的代码中导入CONSTANT或临时导入CONSTANT。 What is the right way to do relative imports? 做相对进口的正确方法是什么?

You are doing it just fine. 你做得很好。 Just import sys in your __init__.py file. 只需在__init__.py文件中导入sys即可。 And fix the spelling of CONSTANT . 并修复CONSTANT的拼写。


Also why do I need the -m flag? 另外为什么我需要-m标志? Isn't it ok to just do python temp from the parent directory of temp? 从temp的父目录中执行python temp是不是可以的?

You need the -m flag to tell that you are using packages. 你需要-m标志来告诉你正在使用包。 If you dont use it you wont be able to do relative imports. 如果你不使用它,你将无法进行相对进口。

You are running inside temp ; temp运行; this is not considered a package and __init__.py is not loaded. 这不被视为包,并且未加载__init__.py Only if the parent of the current directory is on the module loading path and you explicitly load temp as a module, is __init__.py loaded. 仅当当前目录的父级位于模块加载路径上并且您显式加载temp作为模块时,才会加载__init__.py

Because temp is not a package you can't use relative imports here. 因为temp 不是包,所以你不能在这里使用相对导入。 Instead, every Python file inside of the directory is considered a top-level module all by themselves. 相反,目录中的每个Python文件都被视为顶层模块。

You'd have move to the parent of the temp directory, then run: 你已经移动到temp目录的父目录,然后运行:

python -m temp

for Python to import temp as a package and then run the __main__ module in that package. 用于Python将temp作为包导入,然后在该包中运行__main__模块。

When you tell Python to run a directory, Python does not treat the directory as a package . 当您告诉Python运行目录时, Python不会将该目录视为包 Instead, Python adds that directory to sys.path and runs its __main__.py . 相反,Python 将该目录添加到sys.path并运行其__main__.py __init__.py is not executed, and relative imports will not view the directory as a package. __init__.py未执行,相对导入不会将目录作为包查看。

If you want to run a package's __main__.py and treat it as part of the package, with __init__.py executed and all, go to the directory containing the package and run 如果你想运行一个包的__main__.py并将其视为包的一部分,并执行了__init__.py ,那么转到包含该包的目录并运行

python -m packagename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM