Import error with module in Python 2.7
-
02-01-2020 - |
Question
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. 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
thanks!
Solution
Click the Start button
, click All Programs
, click Accessories
, and then click Command Prompt
.
Type Python
then hit 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. Close the command prompt and open another one.
In the new command prompt type: cd C:\Users\User\Documents\Python\xlrddocs
Then type: Python setup.py install
That's it! Now in your .py file type:
import os
import xlrd
OTHER TIPS
The xlrd package is not installed. For example, you can do
$yum install python27_xlrd-0.9.3