相关疑难解决方法(0)

从脚本导入已安装的包会引发"AttributeError:module has no attribute"或"ImportError:无法导入名称"

我有一个名为的脚本requests.py导入请求包.该脚本无法访问包中的属性,也无法导入它们.为什么这不起作用,我该如何解决?

以下代码提出了一个问题AttributeError.

import requests

res = requests.get('http://www.google.ca')
print(res)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
  File "/Users/me/dev/rough/requests.py", line 1, in <module>
    import requests
  File "/Users/me/dev/rough/requests.py", line 3, in <module>
    requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)

以下代码提出了一个问题ImportError.

from requests import get

res = get('http://www.google.ca')
print(res)
Run Code Online (Sandbox Code Playgroud)
Traceback (most recent call last):
  File "requests.py", line 1, in <module>
    from requests import get
  File "/Users/me/dev/rough/requests.py", line 1, in <module>
    from requests import get
ImportError: cannot import name 'get' …
Run Code Online (Sandbox Code Playgroud)

python exception python-module shadowing

43
推荐指数
2
解决办法
1万
查看次数

Django:导入错误:无法从“celery”导入名称“Celery”

所以我已经安装了celery该文件并celery_tasks_settings.py在我的项目目录中的settings.py. 我得到的这是一种避免与包本身冲突的方法。

我也做了

find -name '*celery*.pyc'
Run Code Online (Sandbox Code Playgroud)

celery.pyc作为查找之前可能生成的任何文件的方法。结果:

/__pycache__/celery_tasks.cpython-38.pyc
/__pycache__/celery_tasks_settings.cpython-38.pyc

Run Code Online (Sandbox Code Playgroud)

不幸的是,我仍然遇到同样的错误:

   from celery import Celery
ImportError: cannot import name 'Celery' from 'celery' 
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?

PS:我也尝试添加:

from __future__ import absolute_import, unicode_literals
Run Code Online (Sandbox Code Playgroud)

给我的celery_tasks_settings.py

我的错误仍然不会改变。[编辑] 完整回溯

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/run/media/marvin/DriveEtCetera/Python/MyProjectsDirectory/super-lamp/.superlamp/lib/python3.8/site-packages/django/core/management/base.py", line 336, in run_from_argv
    connections.close_all()
  File …
Run Code Online (Sandbox Code Playgroud)

python django celery

7
推荐指数
1
解决办法
6598
查看次数

标签 统计

python ×2

celery ×1

django ×1

exception ×1

python-module ×1

shadowing ×1