简体   繁体   English

python导入根文件夹并使用所有子文件夹

[英]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.

相关问题 如何将所有子文件夹复制到Python 3中的现有文件夹? - How to copy all subfolders into an existing folder in Python 3? Python:更改所有文件夹和子文件夹中的文件名和文件夹名 - Python: Changing filenames and folder names in all folders and subfolders 如何将所有子文件夹中的图片移动到 Python 中另一个新的相同文件夹中? - How to move the pictures in all subfolders to another new same folder in Python? Python脚本以递归方式重命名文件夹和子文件夹中的所有文件 - Python script recursively rename all files in folder and subfolders Python从文件夹中导入所有内容 - Python import all from folder Pycharm:将文件夹标记为“sources root”对于子文件夹不是递归的 - Pycharm: Marking a folder as 'sources root' is not recursive for subfolders Python3:最小化文件夹中的子文件夹 - Python3: Minimze subfolders in folder 如何在python中使用os.walk更改根文件夹和所有子目录中的ext? - How do i use os.walk in python to change ext within root folder and all subdirectories? 从Python文件夹导入和读取所有文件 - import and read all files from a folder Python 自动导入给定文件夹中的所有Python文件? - Automatically import to all Python files in the given folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM