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.

4

3 に答える 3

0

私が理解していることから、ファイルはロードする前にコンパイルpyxする必要があります。最初に発行した場合、を使用してスクリプト内からこれを行うことができます。pyximport

import pyximport; pyximport.install(pyimport = True)

その上、あなたのパスに別のものがあるように見えるという事実に基づいてreader.py、私はあなたが存在するのと同じディレクトリにフォルダを作成することをお勧めしmain.pyます (あなたはそれに名前を付けますtest_imports) 。次を発行するときに、これらのファイルをインポートしていることを確認してください。reader.pyxwriter.pyx

from test_imports import reader, writer

ディレクトリには、Python がpackagetest_importsであることを示す空のファイルも必要になることに注意してください。__init__.py

于 2013-05-07T18:37:12.097 に答える
0

私が見る2つのオプションがあります。まず、PYTHONPATH が Eclipse と Windows で異なる可能性があることを覚えておいてください。Eclipses はパスを変更します。第二に、これを試してください

from writer import *

ライターパッケージが見つかるかどうかをお知らせください

于 2013-05-07T14:24:08.800 に答える