Python does not detect .pyc files

五迷三道 提交于 2019-12-08 02:32:36

问题


I am using Python 3.2 (both for building and executing), and here is my question.

I intend to ship my python application with the following setup:

There is a main script (say, Main.py), that is using a compiled module, say Module1.pyc). To be precise, the directory structure is:

.\Main.py
.\__pycache__\Module1.cpython-32.pyc

When I use the python interpreter to run the main script, it fails to find the module with the following error:

Traceback (most recent call last):
  File "Main.py", line 10, in <module>
    import Module1
ImportError: No module named Module1

Note that I have added the current directory to PYTHONPATH environment variable, and is part of sys.path. Also, the inner __pycache__ directory is also added, and is visible in sys.path.

Not sure why Module1 is not found. Am guessing, it could be because of the different file name - Module1.cpython-32.pyc? But, then that is how the Python 3.2 interpreter generates it.


回答1:


Have a look at PEP-3147. They describe how the python-lookup mechanism works.

So in your concrete case: Put the file Module1.pyc directly in the root folder.




回答2:


As stated below, two steps resolved the issue: Step 1: Copy the Module.cpython-32.pyc file from .__pycache__ directory to .\ Step 2: Rename the file to Module.pyc

PS: Thanks to gecco for sharing the detail.



来源:https://stackoverflow.com/questions/11648440/python-does-not-detect-pyc-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!