3

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>

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

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

Thanks

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

so for every module in the subpackage1 I would have to do an import in the init.py of subpackage?
You would have to. You may also import main.subpackage1.admin as admin and then in your code you can use admin.PrintAdmin()
I know no better way for static import. Would like to know as well
0

Unless I completely misunderstood what you were asking, due to the way Python importing works, if you do

from mainpackage import subpackage1

All the items in subpackage1 can be referenced without putting mainpackage in front of it, as opposed to if you were to do

import mainpackage

1 Comment

ok, i think I see what you are saying. So I'm just misunderstanding imports?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.