导入错误:从其他 Python 脚本调用时没有名为 xmltodict 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23919161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: No module named xmltodict while calling from other Python script
提问by Volatil3
I am having a weird problem.
我有一个奇怪的问题。
I am on Py2.7 and I am calling a py file from python script. Below is my code
我在 Py2.7 上,我正在从 python 脚本调用一个 py 文件。下面是我的代码
caller.py
调用者.py
import os
import subprocess
filename = 'file.py'
data = 'aXD'
output = subprocess.check_output(['python', filename,data], shell=False)
file.py
文件.py
import sys
import os
import xmltodict
args = sys.argv
xml = args[1].strip('\n')
xml = xml.strip()
pid = str(os.getpid())
result = {'msg':'ok',"pid":pid}
print(result)
And it gives error:
它给出了错误:
import xmltodict
ImportError: No module named xmltodict
Traceback (most recent call last):
The module is RIGHT there since the file runs perfect when executing individually.
该模块就在那里,因为文件在单独执行时运行良好。
采纳答案by g.d.d.c
As an answer, instead of in comments -> the issue is that you've got more than one python interpreter installed and you're getting a different one than you expected when you launched it via subprocess.check_output
. You should address that by changing your invocation like so:
作为答案,而不是在评论中 -> 问题是您安装了不止一个 python 解释器,并且当您通过subprocess.check_output
. 你应该通过像这样改变你的调用来解决这个问题:
output = subprocess.check_output([sys.executable, filename,data], shell=False)
Which will ensure, at the very least, that both scripts are run by the same interpreter.
这将至少确保两个脚本由同一个解释器运行。
回答by johntellsall
Add "current directory" to your Python path so it will find modules in the directory alongside the main program
将“当前目录”添加到您的 Python 路径,以便它会在主程序旁边的目录中找到模块
import sys
sys.path.append('.')
回答by Tsanko
The issue can be resolved trough this simple way:
use 'pip' - python package manager
$ sudo pip install xmltodict
这个问题可以通过这种简单的方式解决:使用'pip'-python包管理器
$ sudo pip install xmltodict
This should install missing module and you shouldn't have problems with this module.
这应该安装缺少的模块,您应该不会遇到此模块的问题。
回答by Brin Brody
Similar to Tsanko's answer, which works for Mac or Linux, for windows, it's nearly the same command, only without the need for sudo.
类似于 Tsanko 的答案,它适用于 Mac 或 Linux,适用于 Windows,它几乎是相同的命令,只是不需要 sudo。
pip install xmltodict
This adaptation worked for me after I had the same issue on a windows computer.
在我在 Windows 计算机上遇到同样的问题后,这种改编对我有用。
回答by Visal Nair
Install this package for xmltodict with conda, you need to run one of the following:
使用 conda 为 xmltodict 安装此包,您需要运行以下其中一项:
conda install -c conda-forge xmltodict
conda install -c conda-forge/label/gcc7 xmltodict
conda install -c conda-forge/label/cf201901 xmltodict
conda install -c conda-forge xmltodict conda install -c conda-forge/label/gcc7 xmltodict conda
install -c conda-forge/label/cf201901 xmltodict
once the package xmltodict is installed, re-run the script. It will work.
安装包 xmltodict 后,重新运行脚本。它会起作用。