python如何调用代码中从未定义的类?

发布于 2024-08-03 23:16:38 字数 529 浏览 5 评论 0原文

我不知道是否可以将所有代码粘贴到此处,但我正在查看 这个 git 存储库

如果您查看示例,他们会这样做:

ec2 = EC2('access key id', 'secret key')

...但是没有 EC2 类。但是,在 libcloud\providers.py 中似乎有一个字典将 EC2 映射到 libcloud\ 中找到的 EC2NodeDriver drivers\ec2.py。正确的映射是由 get_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

御守 2024-08-10 23:16:38

example.py 包含一个 import 语句,内容如下:

from libcloud.drivers import EC2, Slicehost, Rackspace

这意味着 EC2 类是从 libcloud.drivers 导入的> 模块。但是,在本例中,libcloud.drivers 实际上是一个(Python 包含模块),意味着 EC2 应该在 libcloud/drivers/ 的文件 __init__.py 中定义,但事实并非如此。这意味着在这种特定情况下,他们的示例代码实际上是错误的。 (我下载了代码,并在运行 example.py 时遇到导入错误,正如您所看到的,文件 libcloud/drivers/__init__.py 不包含任何定义至少,至少是 EC2 定义。)

example.py includes an import statement that reads:

from libcloud.drivers import EC2, Slicehost, Rackspace

This means that the EC2 class is imported from the libcloud.drivers module. However, in this case, libcloud.drivers is actually a package (a Python package contains modules), which means that EC2 should be defined in a file __init__.py in libcloud/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 running example.py, and as you can see, the file libcloud/drivers/__init__.py does not contain any definitions at all, least of all an EC2 definition.)

木格 2024-08-10 23:16:38

查看 libcloud\examples.py 可能会有所帮助。我看到了这个:

from libcloud.drivers import EC2, Slicehost, Rackspace

python 'import' 语句从其他 python 模块引入类,在本例中是从 libcloud.drivers 模块引入类。

Checking out the libcloud\examples.py might be helpful. I saw this:

from libcloud.drivers import EC2, Slicehost, Rackspace

The python 'import' statement brings in the class from other python module, in this case from the libcloud.drivers module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文