PYVISA中的ENUM模块和cStringIO模块

时间:2015-11-09 08:07:25

标签: python python-3.x visa gpib cstringio

我有一些麻烦需要解决。我使用Python 3.2和pyvisa用于Python 3.2 32位。我用的时候:

import pyvisa

显示:

ImportError: No module named enum

但是当我使用时:

import pyqtgraph, pyvisa

我明白了:

ImportError: No module named cStringIO

我只想使用pyvisa来使用GPIB的Agilent 33250a。

1 个答案:

答案 0 :(得分:0)

The enum module wasn't part of Python until Python 3.4, so 3.2 is too early; you need to upgrade, or you need to live without enum (upgrading is a good idea mind you; the performance and features of Python have improved markedly since then; on performance in particular, strings and user defined class instances dramatically reduced their memory overhead). I'm guessing pyvisa dropped support for Python versions older than 3.4 if they're depending on enum.

cStringIO is a Python 2.x only accelerator module for StringIO; in Python 3.0 and higher, you just import io and use io.StringIO, and it will automatically use the C accelerated code under the hood when available, and pure Python code otherwise. If you're only targeting Python 3, just do import io or from io import StringIO. For code that should run under both Python 2 and Python 3, and use str in both, you can do the following for imports:

try:
    from cStringIO import StringIO  # Py2 C accelerated version
except ImportError:
    try:
        from StringIO import StringIO  # Py2 fallback version
    except ImportError:
        from io import StringIO  # Py3 version

If you want to handle Unicode text regardless of Python version (well, in 2.6 and up), you can just use io.StringIO exclusively; it works with unicode in Py2, and str in Py3, which means it handles all text in both versions (where cStringIO only handles str in Py2, so it can't handle the whole Unicode range).

I suspect your other import error for pyqtgraph would be because you tried installing a version of pyqtgraph written for Python 2; the pyqtgraph page claims Python 3.x compatibility, and use of cStringIO without a fallback would not meet that claim, so either you installed the wrong version, or it was installed incorrectly (e.g. if they were using a single code base and 2to3-ing it, but you somehow installed it without 2to3-ing it; no idea how you'd do that).