[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.python

execute python script question

Gabriel Rossetti

3/10/2008 5:55:00 PM

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

--
www.mydeskfriend.com
PSE - C (EPFL)
1015 Ecublens, Switzerland
Tel: +41 21 601 52 76
Mob: +41 76 442 71 62

2 Answers

samuel.progin

3/10/2008 9:30:00 PM

0

Hello,

I may misunderstand your problem, but it may be related to the
execution environment, especially the PYTHONPATH variable. Have a look
at the following log:

samuel@Bioman2:/$ pwd
/
samuel@Bioman2:/$ cat -n /tmp/test_import.py
1 class A(object):
2 def __init__(self):
3 self.value = 1
4 def show(self):
5 print self.value
samuel@Bioman2:/$ python
Python 2.5.1 (r251:54863, Oct 5 2007, 13:50:07)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test_import import A
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named test_import
>>> exit()
samuel@Bioman2:/$ export PYTHONPATH=/tmp
samuel@Bioman2:/$ python
Python 2.5.1 (r251:54863, Oct 5 2007, 13:50:07)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test_import import A
>>> a=A()
>>> a.show()
1
>>>

++

Sam

Gabriel Rossetti

3/11/2008 8:49:00 AM

0


Sam wrote:
> Hello,
>
> I may misunderstand your problem, but it may be related to the
> execution environment, especially the PYTHONPATH variable. Have a look
> at the following log:
>
> samuel@Bioman2:/$ pwd
> /
> samuel@Bioman2:/$ cat -n /tmp/test_import.py
> 1 class A(object):
> 2 def __init__(self):
> 3 self.value = 1
> 4 def show(self):
> 5 print self.value
> samuel@Bioman2:/$ python
> Python 2.5.1 (r251:54863, Oct 5 2007, 13:50:07)
> [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
>>>> from test_import import A
>>>>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ImportError: No module named test_import
>
>>>> exit()
>>>>
> samuel@Bioman2:/$ export PYTHONPATH=/tmp
> samuel@Bioman2:/$ python
> Python 2.5.1 (r251:54863, Oct 5 2007, 13:50:07)
> [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
>>>> from test_import import A
>>>> a=A()
>>>> a.show()
>>>>
> 1
>
>
> ++
>
> Sam
>
Hello Sam,

Thank you for your reply. I tried that and it works, thanks. I was
trying to modify the sys.path in __init__.py and it wasn't working.

Gabriel