自定义python包的导入问题 [英] Import issues with custom python package

查看:28
本文介绍了自定义python包的导入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样存根的python包:

I have a python package that is stubbed out like this:

<main package>
   |-> __init__.py
   <sub package1>
      |-> __init__.py
      |-> admin.py
      |-> <other python files>
   <sub package 2>
      |-> __init__.py
      |-> <other python files>

在主程序包 init .py中,我执行了以下操作:

in the main package init.py I did the following:

import subpackage1
import subpackage2
__version__ = "1.2.1a"

当我使用该软件包时,遇到导入问题

When I go to use the package, I run into issue with imports

from mainpackage import subpackage1 # works 
admin = subpackage1.admin  #fails
from mainpackage.subpackage1 import admin # works

我应该能够直接从subpackage1调用管理模块吗?有什么我想念的吗?

Should I be able to directly call the admin module from subpackage1? Is there something I'm missing?

谢谢

推荐答案

尝试在 subpackage1 __ init __.py 中添加 import admin .之后,下面的代码应该起作用:

Try to add import admin in __init__.py of the subpackage1. After that the following code should work:

/main/subpackage1/__init__.py

import admin

/main/subpackage1/admin.py

def PrintAdmin():
    print 'Admin'

/some_other_module.py:

from main import subpackage1
admin = subpackage1.admin 
#Invoke some function from admin.py
admin.PrintAdmin() #RESULT: Admin

这篇关于自定义python包的导入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆