English 中文(简体)
python-pyusb导入usb.core不起作用
原标题:python pyusb import usb.core doesn t work

我正在学习教程(http://pyusb.sourceforge.net/docs/1.0/tutorial.html)

I am on windows xp sp3, my python version is 2.7 and I downloaded and installed the pyusb-1.0.0-a1.zip

和libusb-win32-bin-1.2.4.0.zip

import usb

工作良好

但是

import usb.core

根本不起作用

上面写着

Traceback (most recent call last):
  File "D:pyusb.py", line 1, in <module>
    from usb import core
  File "D:pyusb.py", line 1, in <module>
    from usb import core
ImportError: cannot import name core

有什么解决方案吗?

谢谢

p.s. "from usb import core" this make

Traceback (most recent call last):
  File "D:pyusb.py", line 1, in <module>
    from usb import core
  File "D:pyusb.py", line 1, in <module>
    from usb import core
ImportError: cannot import name core

完整的源代码在这里

from usb import core
#find device
dev = usb.core.find(idVendor=0x1516, idProduct=0x8628)
#found?
if dev is None :
        raise ValueError( device not found )

#set the active config. with no args, the first config will be the active one

dev.set_configuration()

#get an end point instance
ep = usb.util.find_descriptor(
    dev.get_interface_altsetting(), #first interface
    #match the first Out Endpoint
    custom_match = 
        lambda e: 
            usb.util.endpoint_direction(e.bEndpointAddress) == 
            usb.util.ENDPOINT_OUT)
assert ep is not None

while(1):
    ep.write(0x5553424350DDBC880000000000000600000000000000000000000000000000)
    ep.write(0x5553425350ddbc880000000000)
最佳回答

在这两种情况下,错误都是:

Traceback (most recent call last):
  File "D:pyusb.py", line 1, in <module>

这意味着它在PATH中的文件usb.py早于python模块的路径(可能在中。在这种情况下是D:py

您是否正确安装了此模块?尝试将这个<code>usb.py</code>文件重命名为其他文件,您会看到错误是否变为“ImportError:No module named usb”。还要检查usb文件夹的Python安装路径(类似于C:Python27),即<;python_path>;libsite软件包susbcore.py

问题回答

你的问题说你使用的是1.0,但我和你有同样的症状,所以我会把这个放在这里,供未来的搜索引擎用户使用。

如果您可以<code>导入usb</code>,但不能<code>导出usb.core</code〕,则可能运行的是python usb 0.x而不是1.0。

https://github.com/walac/pyusb

我假设“D:pyusb.py”是您的py测试程序的名称。

不幸的是,由于usb也是模块的名称,这使得py编译器感到困惑。

在usbtest.py中更改它,一切正常

为了理解python要导入模块的位置,您可以运行以下代码:

import sys
print(sys.path)

这将显示python搜索要导入的模块的目录名列表:)





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...