tags:

views:

72

answers:

1

Hi there !

I have a project I build on a library I'm building in parallel. The structure is the following :

project/
  main.py
  MyLibrary/
    __init__.py --> empty
      Module1.py --> contain the class Class1
      Module2.py --> contain the class Class2
      Module3.py --> contain the class Class3
      ...

I need to import the class Class2 into Class1.py, if I do

from Module1 import Class1

it's ok. But when in project/main.py I do

from MyLibrary import Module1

I got ImportError: No module name Module1

I could solve the issue by replacing:

from Module1 import Class1

by

from .Module1 import Class1

But then I'm not able any more to run Module1.py direcly (Python complain that I'm trying to do a relative impact on a non package...). And I need that to run test.

How I could I have import that work in both case ?

Thank for your attention !

edit: the first described solution seems to work on my linux desktop but not on my windows Xp powerred laptop. It's weird...

+1  A: 

The behavior you're experiencing (where imports work normally on one machine and not another) often happens because you've got multiple packages named MyLibrary on one system and your PYTHONPATH doesn't list '.' first.

To test if this is the problem, in the project directory, run Python and do

>>> import MyLibrary
>>> print MyLibrary

and see if the path to MyLibrary matches your expectations. If it doesn't, tweak your PYTHONPATH and/or delete the outdated version of your package.

A common way to avoid this sort of problem is to use virtualenv to create an isolated Python environment.

Jeffrey Harris