Skip to main content
 首页 » 编程设计

python之我收到此错误 : "ImportError: cannot import name python" How do I fix it

2025年05月04日11飞鱼

File "G:\Python25\Lib\site-packages\PyAMF-0.6b2-py2.5-win32.egg\pyamf\util\__init__.py", line 15, in <module> 
ImportError: cannot import name python 

我该如何解决? 如果您需要任何信息来了解如何解决此问题,我可以解释,尽管问。

谢谢

代码:

from google.appengine.ext.webapp.util import run_wsgi_app 
from google.appengine.ext import webapp 
from TottysGateway import TottysGateway 
import logging 
 
def main(): 
    services_root = 'services' 
    #services = ['users.login'] 
 
    #gateway = TottysGateway(services, services_root, logger=logging, debug=True) 
 
    #app = webapp.WSGIApplication([('/', gateway)], debug=True) 
 
    #run_wsgi_app(app) 
 
if __name__ == "__main__": 
    main() 

代码:

from pyamf.remoting.gateway.google import WebAppGateway 
import logging 
 
 
class TottysGateway(WebAppGateway): 
    def __init__(self, services_available, root_path, not_found_service, logger, debug): 
        # override the contructor and then call the super 
        self.services_available = services_available 
        self.root_path = root_path 
        self.not_found_service = not_found_service 
        WebAppGateway.__init__(self, {}, logger=logging, debug=True) 
 
    def getServiceRequest(self, request, target): 
        # override the original getServiceRequest method 
        try: 
            # try looking for the service in the services list 
            return WebAppGateway.getServiceRequest(self, request, target) 
        except: 
            pass 
 
        try: 
            # don't know what it does but is an error for now 
            service_func = self.router(target) 
        except: 
            if(target in self.services_available): 
                # only if is an available service import it's module 
                # so it doesn't access services that should be hidden 
                try: 
                    module_path = self.root_path + '.' + target 
                    paths = target.rsplit('.') 
                    func_name = paths[len(paths) - 1] 
                    import_as = '_'.join(paths) + '_' + func_name 
                    import_string = "from "+module_path+" import "+func_name+' as service_func' 
                    exec import_string 
                except: 
                    service_func = False 
 
        if(not service_func): 
            # if is not found load the default not found service 
            module_path = self.rootPath + '.' + self.not_found_service 
            import_string = "from "+module_path+" import "+func_name+' as service_func' 
 
        # add the service loaded above 
        assign_string = "self.addService(service_func, target)" 
        exec assign_string 
 
        return WebAppGateway.getServiceRequest(self, request, target) 

请您参考如下方法:

您需要发布完整的回溯。你在这里展示的东西并不是那么有用。我最终挖掘了 pyamf/util/init.py 的第 15 行。您应该发布的代码是

from pyamf import python 

除非您的本地环境一团糟,否则这应该不会失败。

你能在交互式 Python shell 中“导入 pyamf.util”和“导入 pyamf.python”吗?如果您在/tmp 中启动 Python 怎么办(假设您在当前目录中可能有一个名为“pyamf.py”的文件。这是一件坏事。)

=(下面的旧评论)=

解决您的问题。我什至不知道 util/__init__.py 的第 15 行应该在哪里。由于我无法弄清楚,所以我无法回答您的问题。相反,我会指出改进您的问题和代码的方法。

首先,正确使用标记语言,让所有的代码都在一个代码块中。确保您已为代码命名,这样我们就知道它来自 util/__init__.py 而不是某个随机文件。

在您的错误消息中,包含完整 回溯,而不是最后两行。

停止在诸如“if(not service_func):”之类的地方使用括号,而是使用空格,所以它是“if not service_func:”。这在 PEP 8 中讨论.

阅读 Python 文档并学习如何使用该语言。类似“func_name = paths[len(paths) - 1]”的东西应该是“func_name = paths[-1]”

了解 import函数,在这种情况下不要使用“exec”。您也不需要“exec assign_string”——只需执行“self.addService(service_func, target)”