0

I am developing a project which has a directory structure like this:

projectName
           package1
              module1.py
           package2
              module2.py

I am developing in Eclipse-PyDev and in module1.py i import module2 using this statement: import projectName.package2.module2

when i want to execute module1 I do: cd projectName python package1.module1

but it can not import module2 giving this error: ImportError: No module named projectName.package2

I'm new to Python and want to know the best practices for packaging and importing.

what i'm doing know is to put module2 in projectName directory and to remove projectName from all imports. This way i can execute the modules. But i have to change the file everytime i copy the development program into the deployment server.

sshashank124
  • 31,495
  • 9
  • 67
  • 76
Ash
  • 3,428
  • 1
  • 34
  • 44
  • 3
    did you create an ____init____.py in package1 and package2? Python requires this, see http://docs.python.org/2/tutorial/modules.html#packages – Gryphius Mar 26 '14 at 13:06
  • 1
    Which version of Python: 2 or 3? – Aaron Digulla Mar 26 '14 at 13:09
  • 1
    possible duplicate of [importing a module in nested packages](http://stackoverflow.com/questions/2183205/importing-a-module-in-nested-packages) – Aaron Digulla Mar 26 '14 at 13:10
  • I'm using Python 2.6. PyDev creates the __init__.py files but they are empty. should i add something to them? What i want is to be able to execute python projectName.package1.module1 without any error while i have imported module2 in module1 using import projectName.package2.module2 – Ash Mar 26 '14 at 13:32
  • 1
    I think @AaronDigulla is right about this being a dupe; see the first answer in the question he pointed to about needing to use absolute imports in a 'main' module. You could do something like 'PYTHONPATH=$PWD python projectName/package1/module1.py' in the directory containing projectName to get the effect you want on Linux; you'd need to 'set PYTHONPATH...' or similar on Windows. – Rory Yorke Mar 26 '14 at 13:42

0 Answers0