开发者

Trouble with PySerial and multiple Python installations

I have Python 2.4.4 and 3.1.3 on my Windows 7 machine. I would like to use PySerial.

I heard that it was built in, so I tried import serial in both versions. Both caused an Import Error.

Then I downloaded the win32 installer from this page. I ran it, and it installed for 2.4.4. (It said it found it from the registry.) I had no option to change to the 3.1.1 version.

I then got the following error in 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

I look up ctypes, and see that it has been bulit in since 2.5.5. Ok.

I download ctypes, and run python setup.py install. (2.4.4.)

I get the following:

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.

Sweet. Ok. That seems like too much effort, so I'd like to try with Python 3.1.1. I edit my PATH environment variable to include C:\Python31\ instead of C:\Python24.

I restart cmd and run python setup.py install on pyserial:

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>

Interesting. I then try to import serial from IDLE in Python 3.1.1, but I get the standard ImportError. Hmm.

What is going on here? Am I doing something wrong?

Update: I tried to run setup.py install using 3.1.1:

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\sc开发者_如何学编程ripts-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>

Hmm... looks like I'm using an incorrect version of pyserial. This download page makes it look like 2.5 is the most current. Odd.

Update 2: I went through and fixed all the syntax errors, but started getting import errors. In __init__.py, I changed:

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

to:

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

Now I'm getting an error in 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

I tried doing the .modulename syntax, but that wouldn't compile.

What else can I do here? I'm having a hard time believing that anyone intended this code to run on Python 3. (Perhaps I got the wrong version?)


First of all, why are you using python 2.4 on Windows? It is pretty old and things are improved (for e.g. ctypes) and you might need those for pyserial.

Now, coming to your installation question, please don't dabble with PYTHONPATH in order to make the module working for one python version vs. another. Python 3 is backwards incompatible, so you won't get it automatically working for what you install for Python 2.x.

There is a simple set of instructions given in the pyserial website:

Download the archive from http://pypi.python.org/pypi/pyserial. Unpack the archive, enter the pyserial-x.y directory and run:

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


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

Note that I am using the interpreter python3 in the second case.


I had a problem similar to yours when I followed the install instructions from the website. Just like it said, I navigated into the unpacked download folder and ran

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

However, when I ran import serial, I got a similar syntax error:

>>> 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

It turns out that you just need to leave the install directory. The download folder has a folder named serial that overrides the newly installed serial module.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜