我目前正在使用PyCharm和Python版本3.4.3进行此特定项目。
此PyCharm之前有Python2.7,我升级到3.4.3。
我正在尝试使用Pandas从Excel文件中获取数据。
这是我的代码:
import pandas as pd
df = pd.read_excel("File.xls", "Sheet1")
print (df)
当我运行此代码时,我收到此错误。
ImportError: No module named 'xlrd'
我搜索了Stackoverflow并找到了一些建议: 我试过
pip install xlrd
但是,当我这样做时,消息说
"Requirement already satisfied: xlrd in ./anaconda2/usr/lib/python2.7/site-packages"
有什么建议吗?
答案 0 :(得分:27)
我遇到了同样的问题。我去了终端(使用Linux),然后输入
sudo pip3 install xlrd
然后我在python中导入xlrd并使用相同的代码:
df = pd.read_excel("File.xlsx", "Sheet1")
print (df)
它对我有用!!
答案 1 :(得分:3)
点击"导入xlrd"旁边的灯泡图标&安培;点击安装包clrd,它会自动安装包
答案 2 :(得分:3)
运行pip install xlrd
完成了安装,但是并没有解决“没有名为xlrd的命名模块”错误。
将xlrd文件夹复制到存储.py程序的文件夹中,解决了此问题。
答案 3 :(得分:2)
对我来说,解决方案是使用xlrd
卸载pip uninstall xlrd
,然后使用pip install xlrd
再次安装。
答案 4 :(得分:0)
如果您位于Bash下的终端或任何其他具有制表符完成功能的半高级shell中,请尝试编写pip
后跟<tab>
。如果我这样做,我会看到写的:
none@vacuum:~$ pip
pip pip3 pip3.5 pip3.6
如您所见,我可以选择仅在pip
下运行pip命令,但我可以选择更新版本的pip。要知道与pip
命令关联的版本(没有其他内容)像往常pip
一样运行--version
或-V
标记。就我而言,pip -V
会产生:
none@vacuum:~$ pip -V
pip 9.0.1 from /usr/local/lib/python3.6/dist-packages (python 3.6)
除此之外,如果您在PyCharm下进行开发,当光标位于无法导入的模块名称下时,您可以按Alt+Enter
以打开允许您安装模块的上下文相关浮动菜单。 (您还可以在Project Interpreter
子菜单下的PyCharm设置菜单中管理特定Python版本的已安装模块列表。)
答案 5 :(得分:0)
由于某些原因,我的linux Mint机器中有python 2.7,3.5和3.6。
我的spyder使用python 3.5,我遇到了同样的问题。我所做的是
/usr/local/lib/python2.7/dist-packages
xlrd
(请注意,要执行此操作,您可以右键单击并以root身份打开)/usr/local/lib/python3.5/dist-packages
或/usr/local/lib/python3.6/dist-packages
并将文件夹xlrd
粘贴到那里。 它对我有用!!!
此方法不会更改默认路径,因此,我仍然可以继续使用python 2.7而不会造成任何伤害(我广泛使用SageMath
之类的东西)
答案 6 :(得分:0)
使用pycharm时,我也遇到了同样的情况,我用pip,pip3和anaconda安装了它,但仍然无法正常工作。
我从以下位置手动安装了软件包
pycharm-> preferences -> project -> project interpreter -> +
而且有效。
答案 7 :(得分:0)
问题似乎是由于系统中有多个python版本,其中一个版本可能满足要求,而另一个版本则无法满足。
在这种情况下,满足python2的要求,但不满足python3的要求,您需要指定下载必须针对python3。
参考上述答案,对我有用的是
python3 -m pip install xlrd
指定python3而不是pip3为我工作。
答案 8 :(得分:0)
如果您使用的是虚拟环境,请使用“ pipenv install xlrd”代替“ pip install xlrd”。应该可以。
答案 9 :(得分:0)
如果您在Windows中遇到问题,请尝试以下对我有用的步骤:
%localappdata%
C:\Users\<YourSystem>\AppData\Local\Programs\Python\Python38-32\Scripts
pip install xlrd
答案 10 :(得分:0)
我在package com.bluehippo.springy;
import com.jayway.restassured.RestAssured;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static com.jayway.restassured.RestAssured.given;
import static com.jayway.restassured.RestAssured.when;
import static org.hamcrest.CoreMatchers.is;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = App.class)
@WebAppConfiguration
@IntegrationTest({"server.port:0",
"spring.datasource.url:jdbc:h2:mem:springy;DB_CLOSE_ON_EXIT=FALSE"})
public class HelloControllerTest {
@Value("${local.server.port}")
int port;
@Before
public void setUp() throws Exception {
RestAssured.port = port;
}
@Test
public void testHello() throws Exception {
when().get("/").then()
.body(is("Hello World!"));
}
@Test
public void testCalc() throws Exception {
given().param("left", 100)
.param("right", 200)
.get("/calc")
.then()
.body("left", is(100))
.body("right", is(200))
.body("answer", is(300));
}
}
- Given that the HelloController:
package com.bluehippo.springy;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
NamedParameterJdbcTemplate jdbcTemplate;
@RequestMapping("/")
String hello() {
return "Hello World!";
}
@Data
static class Result {
private final int left;
private final int right;
private final long answer;
public Result (int left, int right, long answer) {
this.left=left;
this.right=right;
this.answer=answer;
if (left+right >500) {
throw new IllegalArgumentException("left+right must be less than 500");
}
}
}
// SQL sample
@RequestMapping("calc")
Result calc(@RequestParam int left, @RequestParam int right) {
MapSqlParameterSource source = new MapSqlParameterSource()
.addValue("left", left)
.addValue("right", right);
return jdbcTemplate.queryForObject("SELECT :left + :right AS answer", source,
(rs, rowNum) -> new Result(left, right, rs.getLong("answer")));
}
}
之后将模块xlrd和xlrd-1.2.0.dist-info复制到项目文件,并且可以正常工作。
答案 11 :(得分:-1)
您必须下载xlrd库,因为熊猫需要它。
在Pycharm中,我将其下载到文件->设置->项目:[项目名称]->项目解释器中