首先安装Python, 我安装的是Python2.7
其次就是安装Pywin32, 在http://sourceforge.net/projects/pywin32/files/pywin32/ 就可以下载到。 注意第一点就是这里的版本是要和python的版本相依赖的,我下载的是pywin32-218.win32-py2.7.exe
编写我自己的程序:
import win32file
import win32con
blabla
blabla
然后执行, 出现的错误是
ImportError: No module named win32con
ImportError: No module named win32file
解决这个问题的时候:有一片文章对我帮助很大
On Wed, Jul 29, 2009 at 3:24 PM, Elias Fotinis <efotinis at yahoo.com> wrote: > First, check if the file > ("<PythonDir>\Lib\site-packages\win32\win32file.pyd") really exists. Then > check the Python module search path. Put a "print sys.path" before the > "import win32file" and check whether it contains the > "...site-packages\win32" dir. > > Either pywin32 didn't install properly or some module messes with your > sys.path. > > > > ----- Original Message ----- From: Iman Darabi > To: python-win32 at python.org > Sent: Wednesday, July 29, 2009 11:52 > Subject: [python-win32] ImportError: No module named win32file > > > i'm using pyserial 2.4 to work with some device via serial port . in linux >> have no prob with that ( because no pywin32 is needed ) but because i should >> write my program portable on both win and linux so tried to test it in win >> ... . installed python2.6 and pywin32-214 >> . but when i try to run my app i get this error : >> ... "C:\Python26\lib\site-packages\serial\serialwin32.py", line 9, in >> <module> >> import win32file >> ImportError : No module named win32file >> >> i tried to search win32file in pywin32 but couldn't find it ! >> i just copied content of build directory to site-packages ... >> does win32file renamed/moved to another module ? >> BTW : i'm not so familiar with windows package installations >> > >
然后我尝试着把sys.path 打印出来, 结果发现真的没有我python安装的地址以及win32file 以及为win32con的地址。 然后自己尝试的设置了pythonpath这个系统变量,没有启作用。
不得已自己在程序中强制指定, Like:
import sys
sys.path.append("/to/path/win32file")
sys.path.append("/to/path/win32con")
import win32file
import win32con
blabla
blabla
这样做也不好,因为为一个这样的程序都要指定。
后来发现python的lib是在 系统环境变量 的path中指定。
终于解决问题。
在Python中使用win32api时遇到了模块找不到的问题。安装了Python2.7和对应版本的Pywin32后,通过打印sys.path发现Python安装路径及win32模块不在路径中。尝试设置PYTHONPATH未成功,改为在代码中直接添加模块路径到sys.path,但这不是理想解决方案。最终,通过将Python的lib路径添加到系统环境变量PATH中解决了问题。
2163

被折叠的 条评论
为什么被折叠?



