Python调用win32api

首先安装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  wrote:

> First, check if the file
> ("\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
>> 
>>   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)