PySerial和多个Python安装的麻烦

时间:2011-03-03 05:33:07

标签: python pyserial

我的Windows 7机器上有Python 2.4.4和3.1.3。我想使用PySerial。

我听说它是​​内置的,所以我在两个版本中尝试了import serial。两者都导致Import Error

然后我从this page下载了win32安装程序。我跑了,它安装了2.4.4。 (它说它是从注册表中找到的。)我无法选择更改为3.1.1版本。

然后我在2.4.4中遇到以下错误:

>>> import serial

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in -toplevel-
    import serial
  File "C:\Python24\Lib\site-packages\serial\__init__.py", line 19, in -toplevel-
    from serialwin32 import *
  File "C:\Python24\Lib\site-packages\serial\serialwin32.py", line 11, in -toplevel-
    import ctypes
ImportError: No module named ctypes

我查看ctypes,并看到它自2.5.5以来一直很大。确定。

我下载ctypes,然后运行python setup.py install。 (2.4.4。)

我得到以下内容:

C:\path\to\ctypes-1.0.2>python setup.py install
running install
running build
running build_py
running build_ext
error: Python was built with Visual Studio 2003;
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2003 was not found on this system. If you have Cygwin installed,
you can try compiling with MingW32, by passing "-c mingw32" to setup.py.

甜。好。这似乎太费劲了,所以我想试试Python 3.1.1。我编辑PATH环境变量以包含C:\Python31\而不是C:\Python24

我重启cmd并在pyserial上运行python setup.py install

C:\path\to\pyserial-2.5>python setup.py install
running install
running build
running build_py
running build_scripts
running install_lib
running install_scripts

C:\path\to\pyserial-2.5>

有趣。然后我尝试在Python 3.1.1中从IDLE中import serial,但我得到了标准ImportError。 HMM。

这里发生了什么?我做错了吗?

更新:我尝试使用3.1.1运行setup.py install

c:\path\to\pyserial-2.5>c:\python31\python.exe setup.py install
running install
running build
running build_py
running build_scripts
creating build\scripts-3.1
copying and adjusting examples\miniterm.py -> build\scripts-3.1
running install_lib
creating c:\python31\Lib\site-packages\serial
copying build\lib\serial\loopback_connection.py -> c:\python31\Lib\site-packages
\serial
copying build\lib\serial\rfc2217.py -> c:\python31\Lib\site-packages\serial
copying build\lib\serial\serialcli.py -> c:\python31\Lib\site-packages\serial
copying build\lib\serial\serialjava.py -> c:\python31\Lib\site-packages\serial
copying build\lib\serial\serialposix.py -> c:\python31\Lib\site-packages\serial
copying build\lib\serial\serialutil.py -> c:\python31\Lib\site-packages\serial
copying build\lib\serial\serialwin32.py -> c:\python31\Lib\site-packages\serial
copying build\lib\serial\sermsdos.py -> c:\python31\Lib\site-packages\serial
copying build\lib\serial\socket_connection.py -> c:\python31\Lib\site-packages\s
erial
copying build\lib\serial\win32.py -> c:\python31\Lib\site-packages\serial
copying build\lib\serial\__init__.py -> c:\python31\Lib\site-packages\serial
byte-compiling c:\python31\Lib\site-packages\serial\loopback_connection.py to lo
opback_connection.pyc
  File "c:\python31\Lib\site-packages\serial\loopback_connection.py", line 101
    except ValueError, e:
                     ^
SyntaxError: invalid syntax

byte-compiling c:\python31\Lib\site-packages\serial\rfc2217.py to rfc2217.pyc
  File "c:\python31\Lib\site-packages\serial\rfc2217.py", line 370
    except Exception, msg:
                    ^
SyntaxError: invalid syntax

byte-compiling c:\python31\Lib\site-packages\serial\serialcli.py to serialcli.py
c
  File "c:\python31\Lib\site-packages\serial\serialcli.py", line 39
    except Exception, msg:
                    ^
SyntaxError: invalid syntax

byte-compiling c:\python31\Lib\site-packages\serial\serialjava.py to serialjava.
pyc
  File "c:\python31\Lib\site-packages\serial\serialjava.py", line 67
    except Exception, msg:
                    ^
SyntaxError: invalid syntax

byte-compiling c:\python31\Lib\site-packages\serial\serialposix.py to serialposi
x.pyc
  File "c:\python31\Lib\site-packages\serial\serialposix.py", line 64
    50:      0000001,
                   ^
SyntaxError: invalid token

byte-compiling c:\python31\Lib\site-packages\serial\serialutil.py to serialutil.
pyc
  File "c:\python31\Lib\site-packages\serial\serialutil.py", line 510
    except TypeError, err:
                    ^
SyntaxError: invalid syntax

byte-compiling c:\python31\Lib\site-packages\serial\serialwin32.py to serialwin3
2.pyc
byte-compiling c:\python31\Lib\site-packages\serial\sermsdos.py to sermsdos.pyc
byte-compiling c:\python31\Lib\site-packages\serial\socket_connection.py to sock
et_connection.pyc
  File "c:\python31\Lib\site-packages\serial\socket_connection.py", line 49
    except Exception, msg:
                    ^
SyntaxError: invalid syntax

byte-compiling c:\python31\Lib\site-packages\serial\win32.py to win32.pyc
  File "c:\python31\Lib\site-packages\serial\win32.py", line 164
    MAXDWORD = 4294967295L # Variable c_uint
                         ^
SyntaxError: invalid syntax

byte-compiling c:\python31\Lib\site-packages\serial\__init__.py to __init__.pyc
running install_scripts
creating c:\python31\Scripts
copying build\scripts-3.1\miniterm.py -> c:\python31\Scripts
running install_egg_info
Writing c:\python31\Lib\site-packages\pyserial_py3k-2.5-py3.1.egg-info


c:\path\to\pyserial-2.5>

嗯......看起来我正在使用pyserial的错误版本。 This download page让它看起来像2.5是最新的。奇

更新2 :我修复了所有语法错误,但开始导致导入错误。在__init__.py中,我更改了:

if os.name == 'nt': #sys.platform == 'win32':
    from serialwin32 import *
elif os.name == 'posix':
    from serialposix import *
elif os.name == 'java':
    from serialjava import *

为:

if os.name == 'nt': #sys.platform == 'win32':
    from .serialwin32 import *
elif os.name == 'posix':
    from .serialposix import *
elif os.name == 'java':
    from .serialjava import *

现在我在serialwin32.py中收到错误:

>>> import serial
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    import serial
  File "C:\Python31\lib\site-packages\serial\__init__.py", line 19, in <module>
    from .serialwin32 import *
  File "C:\Python31\lib\site-packages\serial\serialwin32.py", line 12, in <module>
    import win32
ImportError: No module named win32

我尝试使用.modulename语法,但这不会编译。

我还能在这做什么?我很难相信任何人都希望这些代码能够在Python 3上运行。(也许我的版本错了?)

2 个答案:

答案 0 :(得分:2)

首先,为什么在Windows上使用python 2.4?它已经很老了,事情也有所改进(比如ctypes),你可能需要那些pyserial。

现在,来到你的安装问题,请不要涉及PYTHONPATH,以使模块适用于一个python版本而不是另一个。 Python 3向后兼容,因此您不会自动为Python 2.x安装的内容工作。

pyserial网站上提供了一套简单的说明:

http://pypi.python.org/pypi/pyserial下载档案。解压缩归档文件,进入pyserial-x.y目录并运行:

# This will be suitable for python2.5
python setup.py install


# This is suitable for python3.1
python3 setup.py install

请注意,我在第二种情况下使用解释器python3

答案 1 :(得分:0)

当我按照网站上的安装说明进行操作时,我遇到了与您类似的问题。就像它说的那样,我导航到解压缩的下载文件夹并运行

# This is suitable for python3.1
python3 setup.py install

但是,当我运行import serial时,我遇到了类似的语法错误:

>>> import serial
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "serial/__init__.py", line 21, in <module>
    from serial.serialposix import *
  File "serial/serialposix.py", line 58
    except IOError, e:
                  ^
SyntaxError: invalid syntax

事实证明你只需要离开安装目录。下载文件夹有一个名为serial的文件夹,它会覆盖新安装的串行模块。