Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

importing modules from subdirs

1 view
Skip to first unread message

Alex Hall

unread,
Mar 11, 2010, 12:51:57 PM3/11/10
to python-list
Hi all,
The manual says, for modules in a project stored in subdirectories, you can do:
import folderName.module

I have a couple questions, though:
1. Do I then have to call functions from module like
folder.module.function, or can I still use the normal module.function?

2. When I try to do this, it fails. I have an sw folder. Inside that I
have a modes folder, holding weather.pyw. Main.pyw, back in the sw
folder, is trying to import modes.weather, but nothing happens. I have
tried putting weather.pyw in its own weather folder under the modes
folder, but that also fails. I have placed an empty __init__.py file
in both the modes folder and the weather subfolder, but I cannot get
main.pyw to import weather!

3. How does weather import from a folder above or beside it? For
example, if a config directory is at the same level as the modes
directory, how can weather import something from config?

Thanks!

--
Have a great day,
Alex (msg sent from GMail website)
meh...@gmail.com; http://www.facebook.com/mehgcap

Gary Herron

unread,
Mar 11, 2010, 1:00:10 PM3/11/10
to python-list
Alex Hall wrote:
> Hi all,
> The manual says, for modules in a project stored in subdirectories, you can do:
> import folderName.module
>
> I have a couple questions, though:
> 1. Do I then have to call functions from module like
> folder.module.function, or can I still use the normal module.function?
>

Either, depending on how you do the import:

import folder.module
folder.module.function()

or

from folder.module import function
function()

or

from folder import module
module.function()

> 2. When I try to do this, it fails. I have an sw folder. Inside that I
> have a modes folder, holding weather.pyw. Main.pyw, back in the sw
> folder, is trying to import modes.weather, but nothing happens. I have
> tried putting weather.pyw in its own weather folder under the modes
> folder, but that also fails. I have placed an empty __init__.py file
> in both the modes folder and the weather subfolder, but I cannot get
> main.pyw to import weather!
>

Show us some code and a diagram of your forcer hierarchy, and we'll look
at it.


> 3. How does weather import from a folder above or beside it? For
> example, if a config directory is at the same level as the modes
> directory, how can weather import something from config?
>

You don't import from up the hierarchy. You can put a higher folder on
sys.path, and get to it that way.

Gary Herron


> Thanks!
>
>

Steve Holden

unread,
Mar 11, 2010, 1:10:48 PM3/11/10
to pytho...@python.org
Alex Hall wrote:
> Hi all,
> The manual says, for modules in a project stored in subdirectories, you can do:
> import folderName.module
>
> I have a couple questions, though:
> 1. Do I then have to call functions from module like
> folder.module.function, or can I still use the normal module.function?
>
> 2. When I try to do this, it fails. I have an sw folder. Inside that I
> have a modes folder, holding weather.pyw. Main.pyw, back in the sw
> folder, is trying to import modes.weather, but nothing happens. I have
> tried putting weather.pyw in its own weather folder under the modes
> folder, but that also fails. I have placed an empty __init__.py file
> in both the modes folder and the weather subfolder, but I cannot get
> main.pyw to import weather!
>
> 3. How does weather import from a folder above or beside it? For
> example, if a config directory is at the same level as the modes
> directory, how can weather import something from config?
>
> Thanks!
>
I haven't checked this, but I believe .pyw names are only for main
programs. Try renaming weather.pyw as weather.py and see if it makes any
difference.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

Alex Hall

unread,
Mar 11, 2010, 2:56:12 PM3/11/10
to Steve Holden, pytho...@python.org
Halfway there. It imports now, but it says that the module does not
have functions which I know it does have. I will just leave it all in
one folder for now and play with organization after I get the project
working better.

> --
> http://mail.python.org/mailman/listinfo/python-list

0 new messages