简体   繁体   English

Python 2.7中的模块导入错误

[英]Import error with module in Python 2.7

I've spent the better part of an afternoon trying to import the xlrd module, it works when i do it in the shell but when i try to run any file I get an import error. 我花了一个下午的大部分时间来尝试导入xlrd模块,当我在外壳中执行该操作时它可以工作,但是当我尝试运行任何文件时却出现导入错误。 Please could somebody provide a solution? 请有人可以提供解决方案吗? (I'm a beginner, so please be excruciatingly specific) (我是一个初学者,因此请特别注意)

This code: 这段代码:

#!/usr/bin/python

import os

os.chdir("C:/Users/User/Documents/Python/xlrd")

import xlrd

returns the error: 返回错误:

Traceback (most recent call last):

   File "C:\Users\User\Documents\Python\Programs\Radiocarbon27.py", line 4 in <module>

import xlrd
ImportError: No module named xlrd

The path of the setup.py which contains the setup.py file is C:\\Users\\User\\Documents\\Python\\xlrddocs 包含setup.py文件的setup.py的路径为C:\\ Users \\ User \\ Documents \\ Python \\ xlrddocs

thanks! 谢谢!

Click the Start button , click All Programs , click Accessories , and then click Command Prompt . 单击Start button ,单击All Programs ,单击Accessories ,然后单击Command Prompt

Type Python then hit enter. 输入Python然后按Enter。

If you get the following, then you have to setup your environment variables. 如果获得以下内容,则必须设置环境变量。

'python' is not recognized as an internal or external command,
    operable program or batch file.

If Python started, then you do not have to setup your environment variables. 如果启动了Python,则不必设置环境变量。 Close the command prompt and open another one. 关闭命令提示符,然后打开另一个。

In the new command prompt type: cd C:\\Users\\User\\Documents\\Python\\xlrddocs 在新的命令提示符下,键入: cd C:\\Users\\User\\Documents\\Python\\xlrddocs

Then type: Python setup.py install 然后输入: Python setup.py install

That's it! 而已! Now in your .py file type: 现在在您的.py文件中输入:

import os
import xlrd

The xlrd package is not installed. 未安装xlrd软件包。 For example, you can do 例如,您可以

$yum install python27_xlrd-0.9.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM