Python 无法导入 openpyxl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34952535/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:45:09  来源:igfitidea点击:

Python cannot import openpyxl

pythonimportopenpyxl

提问by EarlyCoder

I am running Windows 7 and using Python 2.7.

我正在运行 Windows 7 并使用 Python 2.7。

I have installed openpyxl using easy_install. It looks like the installation was successful. I changed the directory and fired up Python.

我已经使用 easy_install 安装了 openpyxl。看起来安装成功了。我更改了目录并启动了 Python。

>>> import openpyxl
>>>

So, this should mean that Python is able to find openpyxl. However, when I execute a simple test program excell_tutorial1.py and run it, I get the following:

所以,这应该意味着 Python 能够找到 openpyxl。但是,当我执行一个简单的测试程序 Excell_tutorial1.py 并运行它时,我得到以下信息:

Traceback (most recent call last):
File "C:/Python27/playground/excell_tutorial1.py", line 7, in <module>
from openpyxl import Workbook
ImportError: No module named openpyxl

Very confusing! It could find it in prompt line but not in the program!

非常混淆!它可以在提示行中找到它,但在程序中找不到!

import os, sys

the_module ="C:\Python27\Lib\site-packages\openpyxl-2.3.3-py2.7.egg\openpyxl"


if the_module not in sys.path:
    sys.path.append(the_module)

if the_module in sys.path:
    print sys.path.index(the_module)
    print sys.path[18]

so, this gives me:

所以,这给了我:

18
C:\Python27\Lib\site-packages\openpyxl-2.3.3-py2.7.egg\openpyxl

Anyone can think of what the problem might be?

任何人都可以想到问题可能是什么?

Much appreciated

非常感激

回答by Collin Stump

Try deleting all openpyxl material from C:\Python27\Lib\site-packages\

尝试从 C:\Python27\Lib\site-packages\ 中删除所有 openpyxl 材料

Once you do that try reinstalling it using pip. (This what worked for me)

完成此操作后,请尝试使用 pip 重新安装它。(这对我有用)

回答by marks

At times this can be a simple permission issue. As it was in my case. I installed it in my local directory with my login.

有时这可能是一个简单的权限问题。就像我的情况一样。我用我的登录名将它安装在我的本地目录中。

python ./setup.py install 

but some of the other user were not able to import the module. They were getting this error:

但其他一些用户无法导入模块。他们收到此错误:

ImportError: No module named openpyxl

Hence I simply gave exe permission to 'others'

因此,我只是将 exe 权限授予“其他人”

chmod -R 755 

That solves the problem at least in my case.

至少在我的情况下,这解决了问题。

回答by brandonbradley

While not quite what you ran into here (since you state that you are using python 2.7), for those who run into this issue and are using python 3, you may be unintentionally installing to python 2 instead. To force the install to python 3 (instead of 2) use pip3 instead.

虽然不是您在这里遇到的情况(因为您声明您使用的是 python 2.7),但对于那些遇到此问题并正在使用 python 3 的人,您可能会无意中安装到 python 2。要强制安装到 python 3(而不是 2),请改用 pip3。

See this thread for more info: No module named 'openpyxl' - Python 3.4 - Ubuntu

有关更多信息,请参阅此线程: No module named 'openpyxl' - Python 3.4 - Ubuntu

回答by rainer

I had the same problem solved using instead of pip or easy install one of the following commands :

我使用代替 pip 或轻松安装以下命令之一解决了同样的问题:

sudo apt-get install python-openpyxl
sudo apt-get install python3-openpyxl

The sudo command also works better for other packages.

sudo 命令也适用于其他软件包。

回答by Rohit Gawas

Go to the directory where pip is installed, for eg.C:\Python27\Scripts and open cmd (simply type cmd in address bar ). Now run the command "pip install openpyxl". It will install the package itself. Hope this will solve your problem.

转到安装 pip 的目录,例如 C:\Python27\Scripts 并打开 cmd(只需在地址栏中键入 cmd )。现在运行命令“pip install openpyxl”。它将安装软件包本身。希望这能解决您的问题。

回答by AYUSH KUMAR

Try this:

尝试这个:

!pip install openpyxl

回答by Arbetraryday

I had the same issue on 3.8.2

我在 3.8.2 上遇到了同样的问题

I found out that python was installed in two locations on my machine (probably py and python, just a guess) Here:

我发现python安装在我机器上的两个位置(可能是py和python,只是猜测)这里:

C:\Users<userAccount>\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8\LocalCache\local-packages\Python38

and Here:

和这里:

C:\Python38

I deleted the one in my C driveand everything is working well now. I would double check to see where your packages are getting installed first, before deleting. Which ever one is being used, keep that one.

我删除了 C 盘中的那个,现在一切正常。在删除之前,我会仔细检查您的软件包的安装位置。正在使用哪一个,保留那个。

For this case, check to see where this package got installed:

对于这种情况,请检查此软件包的安装位置

C:\Users\<userAccount>\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8\LocalCache\local-packages\Python38\site-packages\openpyxl

keep that directory.

保留那个目录。