Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36928

Importing Classes from child folders.

X-Received by 10.224.110.68 with SMTP id m4mr2625227qap.2.1358408246931; Wed, 16 Jan 2013 23:37:26 -0800 (PST)
X-Received by 10.49.60.40 with SMTP id e8mr918787qer.40.1358408246830; Wed, 16 Jan 2013 23:37:26 -0800 (PST)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!p13no933314qai.0!news-out.google.com!k2ni675qap.0!nntp.google.com!p13no933309qai.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Wed, 16 Jan 2013 23:37:26 -0800 (PST)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=69.94.201.224; posting-account=VNK-2woAAABDbQncdlZ46hFSCueBMPjZ
NNTP-Posting-Host 69.94.201.224
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <e69edbd2-d472-4e69-8725-505b388e5e72@googlegroups.com> (permalink)
Subject Importing Classes from child folders.
From monosij.forums@gmail.com
Injection-Date Thu, 17 Jan 2013 07:37:26 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:36928

Show key headers only | View raw


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.path.dirname(__file__), os.path.pardir)))
from service.SystemSetup import SystemSetup
...
No luck.

Thank you again.

Monosij

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Importing Classes from child folders. monosij.forums@gmail.com - 2013-01-16 23:37 -0800
  Re: Importing Classes from child folders. "Tobias M." <tm@tobix.eu> - 2013-01-17 10:31 +0100

csiph-web