Reputation: 373
I have a python project with this structure: (This is not a real project, only for testing)
ImportTest
ImportPersonsTest\
ImportPerson\
ImportPerson.py
RunImportPersonTest.py
RunImportTests.py
I want this tests to call each other. E.g : RunImportTests.py calls a method in RunImportPersonTest.py, and RunImportPersonTest.py calls a method ImportPerson.py
RunImportPersonTest:
import os
import sys
sys.path.insert(0, os.getcwd() + "../../../")
from ImportPerson import ImportPerson
RunImportTests
import os
import sys
sys.path.insert(0, os.getcwd() + "../../")
from ImportPersonsTest import RunImportsPersonTest
I have success when I run ImportPerson.py and RunImportPersonTest.py, but when I try to run RunImportTests I get this error :
Traceback (most recent call last):
File "xxx\LiClipse Workspace\SystemTest\ImportTest\RunImportTests.py", line 4, in <module>
from ImportPersonsTest import RunImportsPersonTest
File "xxx\LiClipse Workspace\SystemTest\ImportTest\ImportPersonsTest\RunImportsPersonTest.py", line 4, in <module>
from ImportPerson import ImportPerson
ImportError: No module named 'ImportPerson'
Any suggestions?
Edit
New Structure
ImportTest
ImportPersonsTest\
ImportPerson\
ImportPerson.py
__init__.py
RunImportPersonTest.py
__init__.py
RunImportTests.py
__init__.py
Upvotes: 0
Views: 168
Reputation: 106
There is two basic problem:
os.getcwd()
as other os
functions return path with no separator at the end. In fact you insert xxx\LiClipse Workspace\SystemTest\ImportTest../../../
which is not a valid path
As mention by @peter, using os.getcwd() is bad idea - it's depend on your location when you run the script. Use:
sys.path.append(os.path.dirname(__file__))
(insert
recommend only at special cases)
But,
It seems that none of this caused your problem. It's only insert bad stuff to your sys.path
. Your importing need to work good cause all the importing done from the self-module-dir, where python firstly search for the requested module.
I copy your package to mine machine - and both runs well! I fix one spelling bug (RunImportsPersonTest -- RunImportPersonTest) - maybe there is other spelling problem
Upvotes: 0
Reputation: 13619
I think your use of os.getcwd() is flawed.
My guess is that you're running your program from the ImportTest directory and so your current working directory will already allow you to do the first import without any need to fix up your path. When you then try the second import, adding ".../ImportTest/../../.." or ".../ImportTest/../.." isn't helping Python find it.
To fix it either add the ImportPersonsTest directory to your path or use a suitably modified name in the import (ensuring you have your init files as already flagged) - e.g.
from ImportPersonsTest.ImportPerson import ImportPerson
Upvotes: 0
Reputation: 59
Looks like you don't have any __init__.py
files in your project. Python needs those files to be able to import modules from folders. The good news is, they are very easy to make: most of the time, they don't need anything in them, they just have to exist.
See: https://docs.python.org/2/tutorial/modules.html#packages
Upvotes: 3