Import error with module in Python 2.7 - python

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!

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

The xlrd package is not installed.
For example, you can do
$yum install python27_xlrd-0.9.3

Related

import module failed with running by console

First of all the project structure looks like this
-- ba-amin-code (main directory)
-- Diverse
-- LanguageIdentification.py
-- TextAlignment
-- FileHandler.py
-- TextHandler.py
I want to import a python file from TextAlignment when im in Diverse.
Project Structure, import statements
This is how I imported it, but im getting this error when running it like this
enter image description here
Is this way wrong to import?
Update:
This is how im importing two py files from TextAlignment.
Im in LanguageIdentification.py
import fasttext
import urllib
import typer
from TextAlignment import TextHandler
from TextAlignment import FileHandler
After that im running the program`from the root-directory (ba-amin-code) by console with this command
python Diverse/LanguageIdentification.py "INPUT.txt"
getting this error
Traceback (most recent call last):
File "C:\Users\Snur\Pycharm Projects\Gitlab\ba-amin-code\Diverse\LanguageIdentification.py", line 4, in <module>
from TextAlignment import TextHandler
ModuleNotFoundError: No module named 'TextAlignment'
The package might be installed in a different venv. Did you try running your code from the pycharm console, with your venv activated?
You have two yellow warnings in pycharm at your imports: what are they?
Otherwise, try the command: $pip list to see if the package is correctly installed in your venv.

When trying to import selenium it does not work

When i attempt to import selenium i get the error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import selenium
ModuleNotFoundError: No module named 'selenium'
My selenium module is currently in:
C:\Users\Maseeek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium
I've seen other people have it in a different directory and want to know how to fix it.
If you are using "PYCHARM" and if you created project with "Virtual Environment" you should choose "Python 3.x.x" instead of "Virtual Environment Python 3.9" that PyCharm created.
If you installed "anaconda" before you need to change paths of libaries.
If you're using PyCharm, then this might help:
PyCharm sometimes shows an error in importing modules that have already been installed (that's annoying). But, try to change the file location to this destination:
C:\Users\Maseeek\PyCharm Projects\(project name)\venv\site-packages\
or try installing the package in PyCharm using python with the following code:
from sys import *
from subprocess import *
call(["-m", "pip", "install", "selenium"])

Getting an error for a module that I definitely imported

import pyinputplus as pyip
while True:
prompt='Want to know how to keep an idiot busy for hours?\n'
response=pyip.inputYesNo(prompt)
if response=='no':
break
print('Thank you. Have a nice day.')
When I run my above code , I get this error:
Traceback (most recent call last):
File "c:\users\XXXXXX\mu_code\idiot.py", line 1, in <module>
import pyinputplus as pyip
File "c:\users\XXXXXX\mu_code\pyinputplus\__init__.py", line 15, in <module>
import pysimplevalidate as pysv # type: ignore
ModuleNotFoundError: No module named 'pysimplevalidate'
I cannot figure it out. The module is definitely installed. I've even moved it from the folder it was originally installed in to the mu folder where the py file is saved. Any help would be appreciated.
The ModuleError says that you do not have pysimplevalidate installed.
Using the same python executable as you are using to run your script (idiot.py), run
python -m pip install pysimplevalidate
or, even more bullet-proof:
<path_to_python.exe> -m pip install pysimplevalidate
If you are not sure what python executable the script is using, you can check it with
# put this on top of your script
import sys
print(sys.executable) # will print C:\path\to\python.exe

Spyder can not import plyfile

I have installed plyfile in the Scripts subdirectory of the Anaconda3 (I run Windows 10), using the pip3. When I enter the command
import plyfile
in the Python 3.5 Shell, the command is executed without problems.
But when I move to Spyder and I enter the same command in the console I receive an error message:
import plyfile
Traceback (most recent call last):
File "<ipython-input-3-db7ef797d821>", line 1, in <module>
import plyfile
ImportError: No module named 'plyfile'
I tried to add the path of the file to the environment using the Windows Commander and giving at the C:\ the command:
sys.path.append(C:\Users\Alexandros_7\Downloads\plyfile-0.4)
but I received the error message:
"sys.path.append" is not recognized as an internal or external command, operable program or batch file
I also tried the following commands from the console of Spyder:
import sys
sys.path.append("C:\\Users\\Alexandros\\Downloads\\plyfile")
These commands were executed without a problem. Then I entered, import plyfile and I received the same error message -- i.e. that "No module named "plyfile"".
Could you please help me?
try give Spyder the same environment paths like Anaconda, in your case the path to the installed plyfile eg.:
your_module is located in: "C:\Users\Alexandros\your_module.py"
in Spyder:
lets add the located path to the system environment
import sys
sys.path.append("C:\Users\Alexandros\") #need string
lets import your module
import your_module
reload(your_module)

Error to launch IPython shell after source install

I have an account in a CentOS server without sudo permission. As a result, I tried to install IPython from source code by python setup.py prefix=<my home directory>.
However, I got the Error:
Traceback (most recent call last):
File "/usr/bin/ipython", line 19, in <module>
from IPython.frontend.terminal.ipapp import launch_new_instance
ImportError: No module named IPython.frontend.terminal.ipapp
I found a question same to mine: IPython import failure and python sys.path in general.
I followed the asker's instruction to add the directory of my IPython to the IPython execution file.
#!/usr/bin/python
"""Terminal-based IPython entry point.
"""
import sys
sys.path.append("./Ipython directory")
from IPython.frontend.terminal.ipapp import launch_new_instance
launch_new_instance()
However, I got the same error as well. So I want to know how can I launch IPython correctly? Thanks.
You probably want to install with the --user flag, which should put it on sys.path automatically.
python setup.py install --user
If you need to use --prefix for some other reason, then make sure that the directory you add to sys.path is the folder containing the IPython package, not the IPython directory. It will probably end with 'site-packages'.
I copied the IPython source folder into ~/bin/. The execution file of IPython can recognize the module in IPython and the IPython shell launches successfully.

Categories

Resources