[英]python import root folder and use all subfolders
With the following dir structure 具有以下目录结构
.
├── setup.py
└── yourpackage
├── __init__.py
├── some_module.py
├── other_module.py
└── sub_package
├── __init__.py
└── more_modules.py
Is it possible to do this: 是否有可能做到这一点:
>> import yourpackage as yp
>> yp.some_module.bar()
>> yp.sub_package.more_modules.foo()
where the contents of some_module
is some_module
的内容在some_module
def bar(): print('bar')
where the contents of more_modules
is more_modules
的内容是more_modules
def foo(): print('foo')
I can't seem to get this to work 我似乎无法让这个工作
EDIT: The error I would get in the first case is 编辑:我在第一种情况下会得到的错误是
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: module 'yourpackage' has no attribute 'some_module'
In the second case, it's a similar error, but with more_modules
在第二种情况下,它是一个类似的错误,但有
more_modules
If you want to use the inner modules without importing them explicitly you have to import them in the __init__.py
file: 如果要使用内部模块而不显式导入它们,则必须在
__init__.py
文件中导入它们:
import some_module
import other_module
import sub_package
and in sub_package/__init__.py
: 并在
sub_package/__init__.py
:
import more_modules
Note that that might make the first import of the module slower. 请注意,这可能会使模块的第一次导入变慢。
Other option is to import them explicitly in your code: 其他选项是在代码中明确导入它们:
>> import yourpackage.some_module
>> import yourpackage.sub_package.more_modules
>> yourpackage.some_module.bar()
>> yourpackage.sub_package.more_modules.foo()
Imports are quite tricky in fact ... 事实上进口非常棘手......
you could do as follows: 你可以这样做:
import yourpackage as yp
from yourpackage import some_module #yp does not work
call in ipython: 在ipython中调用:
In [8]: yp.some_module.bar()
bar
then 然后
import yourpackage.sub_package
from yourpackage.sub_package import more_modules
call in ipython: 在ipython中调用:
In [20]: yp.sub_package.more_modules.foo()
foo
You have to import top package then subpackages ... 你必须导入顶级包然后子包......
Here's an interesting link: https://www.codementor.io/sheena/python-path-virtualenv-import-for-beginners-du107r3o1 这是一个有趣的链接: https : //www.codementor.io/sheena/python-path-virtualenv-import-for-beginners-du107r3o1
and this one as well :) : https://docs.python.org/2/tutorial/modules.html 这个也是:): https : //docs.python.org/2/tutorial/modules.html
To pile on the answer of @A.Joly and @slallum: actually, I found a combination of their methods actually works. 为了得到@ A.Joly和@slallum的答案:实际上,我发现他们的方法组合实际上是有效的。 I created these:
我创造了这些:
pycharmprojects
|_networkxy
|_ main_run.py ---- misleading name, it's just an ordinary file
|_ more_module.py
|_ __init__.py
content of __init__.py
: __init__.py
内容:
from . import main_run
from . import more_module
content of main_run.py
: main_run.py
内容:
def some_module():
print('some module')
content of more_module.py
: more_module.py
内容:
def more_module():
print('more module')
Now, at directory pycharmprojects
, in command line: 现在,在目录
pycharmprojects
,在命令行中:
(C:\Users\Name\.conda\envs\my_root) C:\Users\Name\PycharmProjects>python
Python 3.6.0 |Anaconda custom (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import networkxy
>>> networkxy.main_run.some_module()
some module
>>> networkxy.more_module.more_module()
more module
Note: for some reasons, I cannot do import main_run
in __init__.py
, but the from . import *
注意:由于某些原因,我不能在
__init__.py
import main_run
,而是from . import *
from . import *
seems to work. from . import *
似乎有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.