简体   繁体   English

如何将函数导入模块的主命名空间?

[英]How to import function to main namespace of my module?

I am using python 2.7, and am building a module with a lot of subdirectories. 我正在使用python 2.7,并且正在构建带有许多子目录的模块。 The structure looks something like this: 结构看起来像这样:

mainmodule/
  __init__.py
  submodule/
    __init__.py
    somefunction.py
    someotherfuntion.py

Now when importing i would, rather than having this: 现在,当我导入时,而不是这样:

>>> from mainmodule.submodule.somefunction import func
>>> func(do_some_stuff)

i want this: 我要这个:

>>> import mainmodule
>>> mainmodule.func(do_some_stuff)

Thanks in advance! 提前致谢!

然后,您应该在主模块的__init__.py导入并公开您的函数:

from .submodule.somefunction import func

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

相关问题 如何将库模块从另一个模块导入我的命名空间? - How to import library modules to my namespace from another module? 如何使用模块= __import __(文件名)并调用主函数 - how to use module = __import__(filename) and invoke the main function 如何从子函数将模块导入main()? - How can I import a module into main() from a sub function? 使 python3 子包模块可用于在主包命名空间中导入 - Make python3 subpackage module available for import in main package namespace 如何将模块导入为 __main__? - How to import a module as __main__? 如何将包含在我的自定义模块中的模块导入 python 交互式环境的命名空间? - How to import module contained in my customized module into python interactive environment's namespace? 当我们导入一个模块时,我们是否只是让主模块可以访问导入模块的命名空间? - When we import a module, are we just making the namespace of the imported module accessible from the main module? 模块命名空间中提供的Python Import Statement和Recursion-need函数 - Python Import Statement and Recursion- need function available in module namespace 如何在导入主模块时模拟函数? - How to mock functions on import of main module? 为什么我不能将我的模块导入到我的主代码中? - Why can't I import my module into my main code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM