Newbie question about import

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Apr 25 22:14:40 EDT 2008


En Fri, 25 Apr 2008 15:03:18 -0300, Luca <lucafbb at gmail.com> escribió:

> Hi all. I'm trying to do something with python import but isn't working  
> for me.
>
> Using python 2,5 I've a program structured like this:
>
> * a main module called (for example) "mommy" with an __init__.py and a
> file called "mommy.py"
> * a __version__ var defined inside the main __init__.py
>
>> From the mommy.py file I need to import the __version__ var, but I'm
> really not able to do this! I fear this is a very stupid task to do...
> my problem is that the file is called like the module.
>
> Anyone can point me to the solution?

The short answer is: don't do that! __init__.py may import any module, but  
other modules in the package should not import anything from __init__.py
The same rule applies to the main module in an application: it can import  
any other required module, but no one should import main.
If you don't follow those rules you may encounter some surprises.
You *can* break the rules and actually do what you want, but I would not  
reccomend it.
In this case, can't you switch the place where __version__ is defined? It  
looks like a constant, so you could have it actually defined in mommy.py,  
and inside __init__.py just import the value.

-- 
Gabriel Genellina




More information about the Python-list mailing list