简体   繁体   English

从Python子目录导入

[英]Importing from Python subdirectories

This should be straight forward, but I'm not sure whether it's my Eclipse config or my Python. 这应该很简单,但是我不确定这是我的Eclipse配置还是我的Python。

I've got a 2 files /trunk/shared/foo/bar.py and /trunk/shared/foo/io.py 我有2个文件/trunk/shared/foo/bar.py和/trunk/shared/foo/io.py

There is an __init__.py there too and in shared. 那里也有一个__init__.py并且共享。

All I want to do is import io.py in bar.py. 我要做的就是在bar.py中导入io.py。 I've tried import io, import foo.io and import shared.foo.io (as well as trunk.shared.foo.io). 我试过导入io,导入foo.io和导入shared.foo.io(以及trunk.shared.foo.io)。 None of these work, failing with the usual unresolved. 这些都不起作用,通常无法解决。

Cheers, Joe 干杯,乔

To allow something like this : import share.foo.bar 为了允许这样的事情: import share.foo.bar

You should have something like this : 您应该有这样的东西:

trunk/
      shared/__init__.py
             foo/__init__.py
                 bar.py
                 io.py

The __init__.py (they must be empty in this case) files are needed to tell to python that it found a python module (ie : something you could import). 需要__init__.py (在这种情况下,它们必须为空)来告诉python它找到了一个python模块(即:您可以导入的东西)。

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

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