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


Groups > comp.lang.python > #36931

Re: Importing Classes from child folders.

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <tm@tobix.eu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'interpreter': 0.04; 'root': 0.04; 'error:': 0.05; 'importerror:': 0.05; 'newbie': 0.05; 'python3': 0.05; 'sys': 0.05; '"as': 0.07; 'class,': 0.07; 'sys,': 0.07; 'python': 0.09; 'directory)': 0.09; 'os.path': 0.09; 'def': 0.10; 'assume': 0.11; 'suggest': 0.11; 'passing': 0.15; "(i'm": 0.16; '__init__.py': 0.16; 'btw': 0.16; 'main():': 0.16; 'received:188.40.28': 0.16; 'received:your- server.de': 0.16; 'references:': 0.16; 'subject:Classes': 0.16; 'sys.path': 0.16; 'tried:': 0.16; 'ubuntu.': 0.16; 'string': 0.17; 'wrote:': 0.17; 'directory.': 0.17; 'have:': 0.17; 'module': 0.19; 'trying': 0.21; 'import': 0.21; 'skip:s 70': 0.22; 'specified': 0.23; 'command': 0.24; 'script': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'appreciated.': 0.26; '[1]': 0.27; 'module.': 0.27; 'run': 0.28; 'initialized': 0.29; 'invoke': 0.29; 'python).': 0.29; 'class': 0.29; 'folder': 0.30; 'url:python': 0.32; 'file': 0.32; 'structure': 0.32; 'running': 0.32; 'could': 0.32; 'to:addr:python-list': 0.33; 'knowledge': 0.33; 'entry': 0.33; 'project': 0.34; 'list': 0.35; 'follows:': 0.35; 'path': 0.35; 'doing': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.36; 'thank': 0.36; 'execute': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'some': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'help': 0.40; 'your': 0.60; 'containing': 0.61; 'first': 0.61; 'different': 0.63; 'email addr:gmail.com': 0.63; 'note:': 0.64; 'first?': 0.84; 'it`s': 0.84; 'standing': 0.84; 'url:path': 0.84; 'controller': 0.91; 'do:': 0.91
Date Thu, 17 Jan 2013 10:31:03 +0100
From "Tobias M." <tm@tobix.eu>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: Importing Classes from child folders.
References <e69edbd2-d472-4e69-8725-505b388e5e72@googlegroups.com>
In-Reply-To <e69edbd2-d472-4e69-8725-505b388e5e72@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Authenticated-Sender tm@tobix.eu
X-Virus-Scanned Clear (ClamAV 0.97.5/16510/Thu Jan 17 06:40:51 2013)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.586.1358415076.2939.python-list@python.org> (permalink)
Lines 75
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358415076 news.xs4all.nl 6924 [2001:888:2000:d::a6]:48587
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36931

Show key headers only | View raw


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, monosij.forums@gmail.com 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.path.dirname(__file__), os.path.pardir)))
> from service.SystemSetup import SystemSetup
> ...
> No luck.
>
> Thank you again.
>
> Monosij

Back to comp.lang.python | Previous | Next — Previous 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