8
I’m trying to run a Python script (v2.4 and v2.7) and created a module with some methods. Unfortunately this module needs to stay in a separate folder.
In the python documentation and so far on Stack Overflow I found numerous post talking to add the __init__.py
in every folder of my project.
My problem is that my script is in one structure and my module is in another. My situation is this:
pasta1/
script.py
pasta2/
modules/
python/
pasta3/
\__init\__.py
modulo1.py
modulo2.py
The part I’m calling these modules in my script is:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import datetime
from pasta3.modulo1 import Mod1
from pasta3.modulo2 import Mod2
The mistake:
$ ./script.py
Traceback (most recent call last):
File "./script.py", line 9, in <module> from pasta3.modulo1 import Mod1
ImportError: No module named pasta3.modulo1
I can’t use the sys.path.append("../modules/python/")
because in the Python 2.4 he is not recognized.
How to proceed in this case?
Import: No module named pasta3.modulo1, the path is wrong, the file importing is where? This structure is right?
– Paulo
Yeah, the structure’s right. In Python 2.7 I was adding sys.path.append("./modules/python/") and it worked, but it was not implemented/works in python 2.4
– bred81
Bred, here we do not use "solved" in the title. Mark an answer as correct or write your own explaining the solution. See How and why to accept an answer?
– brasofilo
thanks. I had not seen the symbol to accept the answer.
– bred81
puts a doc init.py inside pasture2 in each of the subfolders, maybe it works not sure, otherwise you will have to put the whole directory to be able to access or abbreviate with the ../
– Milton Luis