python如何调用代码中从未定义的类?
我不知道是否可以将所有代码粘贴到此处,但我正在查看 这个 git 存储库。
如果您查看示例,他们会这样做:
ec2 = EC2('access key id', 'secret key')
...但是没有 EC2
类。但是,在 libcloud\providers.py
中似乎有一个字典将 EC2
映射到 libcloud\ 中找到的
。正确的映射是由 EC2NodeDriver
drivers\ec2.pyget_driver(provider)
计算的,但该方法似乎没有在任何地方被调用。
显然,我对 python 很陌生,但对编程却不是。我什至不确定我应该在文档中查找什么来解决这个问题。
I don't know if it is feasable to paste all of the code here but I am looking at the code in this git repo.
If you look at the example they do:
ec2 = EC2('access key id', 'secret key')
...but there is no EC2
class. However, it looks like in libcloud\providers.py
there is a dict that maps the EC2
to the EC2NodeDriver
found in libcloud\drivers\ec2.py
. The correct mapping is calculated by get_driver(provider)
, but that method doesn't appear to be called anywhere.
I am new to python, obviously, but not to programming. I'm not even sure what I should be looking up in the docs to figure this out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
example.py
包含一个import
语句,内容如下:这意味着
EC2
类是从libcloud.drivers
导入的> 模块。但是,在本例中,libcloud.drivers
实际上是一个包(Python 包包含模块),意味着EC2
应该在libcloud/drivers/
的文件__init__.py
中定义,但事实并非如此。这意味着在这种特定情况下,他们的示例代码实际上是错误的。 (我下载了代码,并在运行example.py
时遇到导入错误,正如您所看到的,文件libcloud/drivers/__init__.py
不包含任何定义至少,至少是EC2
定义。)example.py
includes animport
statement that reads:This means that the
EC2
class is imported from thelibcloud.drivers
module. However, in this case,libcloud.drivers
is actually a package (a Python package contains modules), which means thatEC2
should be defined in a file__init__.py
inlibcloud/drivers/
, but it's not. Which means that in this specific case, their example code is actually wrong. (I downloaded the code and got an import error when runningexample.py
, and as you can see, the filelibcloud/drivers/__init__.py
does not contain any definitions at all, least of all anEC2
definition.)查看 libcloud\examples.py 可能会有所帮助。我看到了这个:
python 'import' 语句从其他 python 模块引入类,在本例中是从 libcloud.drivers 模块引入类。
Checking out the libcloud\examples.py might be helpful. I saw this:
The python 'import' statement brings in the class from other python module, in this case from the libcloud.drivers module.