0

I am working in:

  • Eclipse
  • Windows 7
  • 64-bit Python 3.3

I want to import writer.pyx (yes, Cython) into main.py. At the top of main.py, I have the appropriate import statement:

import writer

Both main.py and writer.pyx are in the same directory, and that directory is also in Windows' PYTHONPATH environment variable. However, it gives me the error ImportError: No module named 'writer'. So, as far as I can tell, it should be working.

But, here's the kicker: in that same directory, there's a file called reader.pyx that I'm also importing in main.py - and it works perfectly. No issues, no errors.

So, clear summary:

  • main.py is importing writer.pyx and reader.pyx
  • All three files are in the same directory (and PYTHONPATH lists that directory)
  • reader.pyx imports fine, but writer.pyx throws an ImportError: No module named 'writer'

Any ideas as to how I can fix this?

Visual representation:

import reader
import writer

def function():
    # code

P.S. This is not my code, and it used to run just fine on this very computer, and the code has not been changed since. This leads me to believe it's an environment problem, but I'm not sure what. Something with Cython, perhaps? I don't have any real experience with it.

Kevin
  • 3
  • 1
  • 3
  • What happens if you get rid of both `writer.pyx` and `reader.pyx` from that directory? Do you still get an error with just `writer.pyx` or do both imports fail now? – fgb May 06 '13 at 21:55
  • @fgb It turns out that there is a `reader.py` module that was importing correctly, and that all `.pyx` files fail - I simply didn't notice this before because it's a huge directory with dozens of modules. This seems like a much simpler and more well defined problem - that being said I'm still not quite sure how to fix it, if you have any ideas. To be clear, I am using someone else's code that was working before and is not now. I shouldn't modify the code in these files if at all possible, so if there's a solution that has to do with the environment, I would appreciate it. Open to all fixes. – Kevin May 07 '13 at 12:36
  • I've followed up in an answer. Hope it helps! – fgb May 07 '13 at 18:38

3 Answers3

0

Are you working in a package? If so you will need to import it using the package name:

from mypackage import writer
underrun
  • 6,713
  • 2
  • 41
  • 53
0

There are two option I see. First, remember that your PYTHONPATH may be different in you eclipse and in your windows. Eclipses changes the path. Second, please try this

from writer import *

and let us know if the writer package will be found

speedingdeer
  • 1,236
  • 2
  • 16
  • 26
  • Thx Kevin, so what I suggest you to do is to try execute your program from command line and from eclipse and check if the error is this same. I think it may be a problem of eclipse PYTHONPATH – speedingdeer May 08 '13 at 07:17
  • I saw above you have already a sollution. I haven't noticed. Sorry for bothering you. – speedingdeer May 08 '13 at 07:19
  • No problem. Thanks for taking the time help! – Kevin May 08 '13 at 12:31
0

From what I understand, pyx files need to be compiled before they can be loaded. You can do this from within your script by using pyximport, if you first issue:

import pyximport; pyximport.install(pyimport = True)

On top of that, based on the fact that there seems to be another reader.py in your path, I'd suggest you create a folder in the same directory where main.py resides (say you name it test_imports) and put both reader.pyx and writer.pyx there, so that you're sure you're importing those files when you issue:

from test_imports import reader, writer

Note that the test_imports directory will also need an empty __init__.py file that tells Python it is a package.

fgb
  • 3,009
  • 1
  • 18
  • 23
  • Well, it lets me import now, thanks (at least for the files that don't cause compiler errors, but that's a whole other topic...). Except that now I get the error `ImportError: DLL load failed: %1 is not a valid Win32 application.` – Kevin May 07 '13 at 18:42
  • That might be related to this question: http://stackoverflow.com/questions/4676433/solving-dll-load-failed-1-is-not-a-valid-win32-application-for-pygame – fgb May 07 '13 at 19:38
  • I looked around and found that, among many other answers, but I'm not sure it'll help me. For context, I just jumped into a large and ongoing project and am figuring everything out still. Changing Python installations or extensions could break other parts of code, so at this point I'll just have to wait for more experienced team members to get back and see what they say. Thanks for all your help! – Kevin May 07 '13 at 19:44
  • I understand. Don't forget that you can mark the answer as accepted by clicking the green checkmark next to it. Good luck figuring out the DLL error. – fgb May 07 '13 at 19:49