471,073 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

Newbie question about import

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?

--
-- luca
Jun 27 '08 #1
2 837
Luca wrote:
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?
In the future please show us what you "actually" did with full tracebacks if
there were any so we know what you actually tried.

I do something like this by doing:

from version import version__

It doesn't have to be a module (e.g. doesn't need __init__.py) to make that work.

Hope this helps.

-Larry
Jun 27 '08 #2
On 25 Apr., 20:03, Luca <luca...@gmail.comwrote:
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?

--
-- luca
You have to import the package containing mummy and __init__ from the
pythonpath ( which can be examined using the sys module and the
sys.path attribute ). Then access __version__ as an attribute:

mypack/ # package indicated by __init__.py
mummy.py
__init__.py
mummy.py
--------
import mypack # o.k. if accessible from pythonpath
mypack.__version__
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by janetb | last post: by
reply views Thread by Gabriel Genellina | last post: by
1 post views Thread by Richard | last post: by
4 posts views Thread by frankrentef | last post: by
2 posts views Thread by Ken D'Ambrosio | last post: by
reply views Thread by Albert-jan Roskam | last post: by
reply views Thread by Robert Kern | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.