Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Importing Classes from child folders.

Reply
Thread Tools

Importing Classes from child folders.

 
 
monosij.forums@gmail.com
Guest
Posts: n/a
 
      01-17-2013
Trying to do some OO Python with files in different directories.
I have a blank __init__.py in each directory.
It is my assumption that having an __init__.py marks the directory as a module.
I am trying to run Controller.py from the command line.

I would assume this has already been solved but could not find in forum
I am somewhat new to Python. Using Python3 under Ubuntu.
....
So I have a project structure as follows:
....
ProjectX (root folder)
__init__.py
Controller.py
+ service (folder under ProjectX)
__init__.py
SystemSetup.py (has a class SystemSetup)
+ model (folder under ProjectX)
__init__.py
Directory.py (has a class Directory)

In Controller.py if I want to use SystemSetup class, I do:
from service.SystemSetup import SystemSetup
....
and in Controller Class I have:
def main():
systemSetup = SystemSetup()

I get error:
File "Controller.py", line 4, in <module>
from service.SystemSetup import SystemSetup
ImportError: cannot import name SystemSetup

What am I doing wrong? I am running from the command line.
Do I need to set the project in PYTHONPATH first?

But if SystemSetup and Directory are in same directory as Controller I have no problems.

Your help would be highly appreciated.
....
Btw I also tried:
import sys, os.path
sys.path.append(os.path.abspath(os.path.join(os.pa th.dirname(__file__), os.path.pardir)))
from service.SystemSetup import SystemSetup
....
No luck.

Thank you again.

Monosij
 
Reply With Quote
 
 
 
 
Tobias M.
Guest
Posts: n/a
 
      01-17-2013
On an import python looks for the module in the directories specified in
sys.path.
The documentation on sys.path says:

"As initialized upon program startup, the first item of this list is the
directory containing the script that was used to invoke the Python
interpreter." [1]

So it`s important that the script you execute by passing it to the
python interpreter is in your root folder ("ProjectX").
If this script is Controller.py, you might want to check the path there by:

import sys
print(sys.path)

There should be an entry with a empty string ("") standing for the
current directory.

Note: This is just what I suggest according to my knowledge (I'm also a
newbie in python).

References:
[1] http://docs.python.org/3.3/library/sys.html#sys.path

On 17.01.2013 08:37, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Trying to do some OO Python with files in different directories.
> I have a blank __init__.py in each directory.
> It is my assumption that having an __init__.py marks the directory as a module.
> I am trying to run Controller.py from the command line.
>
> I would assume this has already been solved but could not find in forum
> I am somewhat new to Python. Using Python3 under Ubuntu.
> ...
> So I have a project structure as follows:
> ...
> ProjectX (root folder)
> __init__.py
> Controller.py
> + service (folder under ProjectX)
> __init__.py
> SystemSetup.py (has a class SystemSetup)
> + model (folder under ProjectX)
> __init__.py
> Directory.py (has a class Directory)
>
> In Controller.py if I want to use SystemSetup class, I do:
> from service.SystemSetup import SystemSetup
> ...
> and in Controller Class I have:
> def main():
> systemSetup = SystemSetup()
>
> I get error:
> File "Controller.py", line 4, in <module>
> from service.SystemSetup import SystemSetup
> ImportError: cannot import name SystemSetup
>
> What am I doing wrong? I am running from the command line.
> Do I need to set the project in PYTHONPATH first?
>
> But if SystemSetup and Directory are in same directory as Controller I have no problems.
>
> Your help would be highly appreciated.
> ...
> Btw I also tried:
> import sys, os.path
> sys.path.append(os.path.abspath(os.path.join(os.pa th.dirname(__file__), os.path.pardir)))
> from service.SystemSetup import SystemSetup
> ...
> No luck.
>
> Thank you again.
>
> Monosij


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing WMI in a child Thread throws an error kyosohma@gmail.com Python 5 03-15-2007 08:18 PM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Can I using reflection to get all child classes or classes undera package dynamically? Carfield Yim Java 1 05-31-2004 05:33 PM
[HELP] Why the child window reopen a new child window ? bhchng ASP .Net 0 01-13-2004 07:17 AM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments