Confused with Python's import intrapackage references -


i run python 2.7.6. having problem creating package. following mwe:

i have following files:

run.py     mypackage/         __init__.py         father_class.py         son_class.py         class1.py 

with following content:

init.py

__all__ = ['father_class', 'son_class', 'class1'] 

run.py

from mypackage import father_class, son_class 

father_class.py:

class daddy(object):     def __init__(self):         self.hello=1 

son_class.py:

from mypackage import * # alternatively tried: mypackage import class1, father_class class sonny(daddy):     def __init__(self):         super(daddy, self).__init__()         self.bye=class1.myclass1()         #alternatively tried self.bye=myclass1() 

class1.py:

class myclass1(object):     def __init__(self):         self.life=1 

when running run.py get:

    self.bye=class1.myclass1() nameerror: global name 'class1' not defined 

any ideas of i'm doing wrong?

you can import modules relatively import unless import available in site-packages globally or in virtualenv. therefore module a in package/a cannot import module b in package/b importing package , navigating there, can import b directly (being on same level):

for son_class.py use:

from class1 import myclass1 father_class import daddy  class sonny(daddy):     def __init__(self):         super(daddy, self).__init__()         self.bye=myclass1() 

otherwise, advisable specific in imports , import definitions need: of enormous anythings needs refactored. if explicitly write down import path receive more sensible error message.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -