[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: execute python script question

Gabriel Rossetti

3/11/2008 8:49:00 AM

Michael Wieher wrote:
> stupid question: you have myPackage somewhere on sys.path?
>
> I mean, module1.py only knows it lives in a directory, it doesn't know
> anything about anything above it.
>
>
>
> 2008/3/10, Gabriel Rossetti <gabriel.rossetti@mydeskfriend.com
> <mailto:gabriel.rossetti@mydeskfriend.com>>:
>
> Hello,
>
> I have been developping something in python that has the following
> hierarchy :
>
> project/src/myPackage/
> project/src/myPackage/__init__.py
> project/src/myPackage/module1.py
> project/src/myPackage/module2.py
> project/src/myPackage/test/
> project/src/myPackage/test/__init__.py
> project/src/myPackage/test/test_module1.py
> project/src/myPackage/test/test_module2.py
> project/src/myPackage/mySubPackage/__init__.py
> project/src/myPackage/mySubPackage/module1.py
> project/src/myPackage/mySubPackage/test/
> project/src/myPackage/mySubPackage/test/__init__.py
> project/src/myPackage/mySubPackage/test/module1.py
> ...
>
> up until now, I had been executing my modules from inside
> project/src/myPackage/
> but I realised that that is wrong (while implementing the test suite)
> and that since all my modules had relative imports (if module2 needed
> module1, it would just say : import module1) I changed them to
> myPackage.module1 for example. Now my test suite is happy, I can say :
> test.sh myPackage.test and it tests everything. The only problem
> now is
> that I can't execute the scripts from inside or outside the myPackage
> dir, I get this :
>
> from outside :
>
> Traceback (most recent call last):
> File "myPackage/module1.py", line 15, in <module>
> from myPackage import constants, utils
> ImportError: No module named myPackage
>
> or if from inside it :
>
> Traceback (most recent call last):
> File "module1.py", line 15, in <module>
> from myPackage import constants, utils
> ImportError: No module named myPackage
>
> can anybody please help me? I don't think I understood the whole
> package/module thing I think... I think some people do some sort of
> importing in the __init__.py files but I'm not sure this helps in
> this case.
>
> Thanks,
> Gabriel
>
>
Hello Michael,

not a stupid question, I think that may be it. I tried setting
PYTHONPATH like Sam suggested and it worked, but I was unable to do it
programmically. I tried putting it in the __init__.py file like a web
post suggested but it wasn't run until after I set PYTHONPATH, and once
that was done there is no need (that I can see anyways) to set it in
__init__.py.

Thanks for your help,
Gabriel
1 Answer

Piet van Oostrum

3/11/2008 10:31:00 AM

0

>>>>> Gabriel Rossetti <gabriel.rossetti@mydeskfriend.com> (GR) wrote:

>GR> not a stupid question, I think that may be it. I tried setting PYTHONPATH
>GR> like Sam suggested and it worked, but I was unable to do it programmically.
>GR> I tried putting it in the __init__.py file like a web post suggested but it
>GR> wasn't run until after I set PYTHONPATH, and once that was done there is no
>GR> need (that I can see anyways) to set it in __init__.py.

The __init__.py is executed during your import statement, thus it is too
late to find MyPackage. You will have to change the sys.path before the
import, e.g. in your main program. Or do what is usually done: put
MyPackage in the site-packages directory.
--
Piet van Oostrum <piet@cs.uu.nl>
URL: http://pietvano... [PGP 8DAE142BE17999C4]
Private email: piet@vanoostrum.org