在我的Django项目中,以下行抛出一个ImportError:"没有名为elementtree的模块".
from elementtree import ElementTree
Run Code Online (Sandbox Code Playgroud)
但是,安装了模块(即,我可以运行交互式python shell,并输入没有任何ImportError的确切行),并且包含该模块的目录位于PYTHONPATH上.但是当我访问浏览器中的任何页面时,它以某种方式找不到该模块,并抛出ImportError.可能是什么导致了这个?
我有一个目录结构:
../POC/mud/
client/
common/
server/
我正在尝试使用以下导入:
from mud.server import config from mud.common.lib import util
但是当我尝试导入配置时,我收到一个错误:
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from mud.server import config Traceback (most recent call last): File "", line 1, in ImportError: No module named mud.server >>> from mud.common.lib import util Traceback (most recent call last): File "", line 1, in ImportError: No module named mud.common.lib >>>
我是否需要在某个位置才能使导入工作,或者需要在操作系统上编译模块?这些"泥"模块只是python .py文件的集合
为了连接到远程MySQL服务器,我试着在我的Mac上设置MySQLdb,我把头发拉到这里.
我试图通过在终端中运行以下命令来设置模块:
$ sudo python setup.py build
$ sudo python setup.py install
这两个命令都失败并出现类似的错误消息.这是第一个堆栈跟踪:
running build
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.6-universal-2.6/MySQLdb
running build_ext
building '_mysql' extension
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/include/mysql -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.6-universal-2.6/_mysql.o -fno-omit-frame-pointer -pipe
In file included from _mysql.c:36:
/usr/include/mysql/my_config.h:1030:2: warning: #warning defining SIZEOF_CHARP = 4
/usr/include/mysql/my_config.h:1044:2: warning: #warning defining SIZEOF_LONG = 4
/usr/libexec/gcc/powerpc-apple-darwin10/4.2.1/as: assembler …Run Code Online (Sandbox Code Playgroud) 我的Windows 7机器上有Python 2.5.x.
os.path.exists('C:') # returns True
os.path.exists('C:\Users') # returns True
os.path.exists('C:\Users\alpha') # returns False, when ALPHA is a user on my machine
Run Code Online (Sandbox Code Playgroud)
我给了我正在使用的CLI的读/写权限.可能的原因是什么?
我试图将一个类的python函数访问另一个脚本。这给了我以下错误:
AttributeError: 'module' object has no attribute 'functionName'
Run Code Online (Sandbox Code Playgroud)
该函数存在于类中,可以通过classname.functionName()调用进行访问。我有什么想念的吗?
-更新-
我的代码是:
(program.py)
import ImageUtils
import ...
class MyFrame(wx.Frame):
...
ImageUtils.ProcessInformation(event)
(ImageUtils.py)
import statements...
class ImageUtils(threading.Thread):
def ProcessInformation(self, event):
self.queue.put(event)
Run Code Online (Sandbox Code Playgroud)
因此,错误是:AttributeError:'module'对象没有属性'ProcessInformation'那么,我是否必须使第二个脚本仅成为模块?
这是我的Python模块:
main.py
fib/
__init__.py
fib.py
hello.py
Run Code Online (Sandbox Code Playgroud)
fib.py定义函数fib(),hello.py定义函数hello().
main.py 是
from fib import *
hello()
Run Code Online (Sandbox Code Playgroud)
__init__.py 是
__all__ = ["fib", "hello"]
Run Code Online (Sandbox Code Playgroud)
我只是为了练习而写这段代码.不用于工作
我跑main.py它打印:
Traceback (most recent call last):
File "tes.py", line 5, in <module>
hello()
TypeError: 'module' object is not callable
Run Code Online (Sandbox Code Playgroud)
为什么?我有名单你好__all__
我需要澄清在同一名称空间中共存时如何处理模块和类级别的导入.请参阅以下示例:
作品:
import datetime
print datetime.datetime.utcnow()
Run Code Online (Sandbox Code Playgroud)
失败:
from datetime import datetime
print datetime.datetime.utcnow()
Run Code Online (Sandbox Code Playgroud)
错误:AttributeError:类型对象'datetime.datetime'没有属性'datetime'
作品:
from datetime import datetime # Is this ignored?
import datetime
print datetime.datetime.utcnow()
Run Code Online (Sandbox Code Playgroud)
第三个例子究竟发生了什么?第二个模块导入是否替换了特定于类的第一个导入?这是否意味着模块和类级别导入不应混合在同一名称空间中?
我想做这样的事情?
# __init__.py
import a_module
__all__ = [
'a_module',
]
__all__.extend(a_module.__all__) # it doesn't work
# AttributeError: 'module' object has no attribute '__all__'
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我正在使用Python.7.10并使用pip安装了beautifulsoup.该软件包已成功安装.但是当我尝试导入beautifulsoup时,我收到了这个错误:
ImportError: No module named beautifulsoup
Run Code Online (Sandbox Code Playgroud)
我查看了已安装模块的列表,并在已安装的模块列表中找到了beautifulsoup模块:



安装Tensorflow后,当我尝试运行我的代码时,我收到一个导入错误
from utils import *
Traceback (most recent call last):
File "<ipython-input-1-b81aa0fdb889>", line 1, in <module>
from utils import *
ImportError: No module named 'utils'
Run Code Online (Sandbox Code Playgroud)
即使我试过了两个
conda install -c conda-forge python-utils
conda install -c conda-forge/label/gcc7 python-utils
Run Code Online (Sandbox Code Playgroud)
如果我查看我的Anaconda Navigator的环境,就会安装python-utils软件包.
我的环境:macOS Mojave 10.14 + Tensorflow 1.12.0 + Python 3.5.6.
任何帮助都非常感谢.
python ×10
python-module ×10
anaconda ×1
django ×1
elementtree ×1
import ×1
macos ×1
mysql ×1
mysql-python ×1
python-2.7 ×1
tensorflow ×1
typeerror ×1