0

I wanted to use full import names in my project (see this question). The problem described there has been solved and everything works fine (edit: if the script is launched with execfile) outside eclipse and pydev (running on windows), in ccps on linux (which has jython built in). However eclipse gives me the following error:

from Project.gui import gui
ImportError: No module named gui

while my project structure inlcudes the following (everything without .py is a folder):

Project
    __init__.py
    gui
        __init__.py
        gui.py
    Project.py

I launch Project.Project.py and get the above error. print sys.path right before the critical import yields:

['****\\Root\\Project', '****\\Root', 
 *bunch of jars/jython libs*,
'__classpath__', '__pyclasspath__/']

I am running eclipse and PyDev.

Community
  • 1
  • 1
ted
  • 4,791
  • 5
  • 38
  • 84
  • could you try renaming Project.py to something else and try again the import? – marlboro Jun 15 '12 at 14:54
  • i just did, that is the solution, for some reason it ignores the package if it sees a module of that name... . Just for the hack of it: is there a way to make python actually search both if one fails? – ted Jun 15 '12 at 15:01
  • "is there a way to make python search for both if one fails" -- Yes. Read up on `try` and `except`. It'll probably look something like: `try: import Project; except ImporError: import Foo as Project` – mgilson Jun 15 '12 at 15:08
  • Why are you giving modules the same names as their parent packages? – Eric Jun 15 '12 at 15:09
  • Because it is a convenice script, it is supposed to start a gui for the provided modules, and i thought this to be more convenient than index.py since I don't know of any standard for a main file, I know changed it to start which I don't like either – ted Jun 15 '12 at 15:21
  • @ted -- Often the "main" file is named `core.py`, but I don't think there's any really established convention for this. Also note that depending on what you've written into `__init__.py`, the user doesn't necessarily need to know the name of the "main" file. – mgilson Jun 15 '12 at 15:27
  • @mgilson well I don't want init to start the gui since I want the user to bea bale to import the whole package for its functionality without starting the gui, thanks for the hint with `core.py` – ted Jun 15 '12 at 15:53
  • @Eric: I know it has been a while, but I just noticed you touched up this question. Thank you, I always look for opportunities to improve my english, so keep those edits up. – ted Oct 22 '12 at 13:02

1 Answers1

2

Because \\Root\\Project is first in your path, it will look inside the Project folder first so from Project.gui sees Project.py, but then throws an error because there's no gui.

You could temporarily put sys.path.insert(0,"****\\Root") in before the import just to check whether this is the case or not. But I'd take ****\\Root\\Project out of your PYTHON_PATH.

then you could do

from Project import Project

and

from Project.gui import gui
GP89
  • 6,600
  • 4
  • 36
  • 64
  • well thanks for a correct solution, I will rename the file though as marius suggested since I consider this saver/easier – ted Jun 15 '12 at 15:22