cant import python module after installation - python-3.x

I am trying to use Pexpect for a python script I am writing with python 3. I tried installing the module on commmand prompt with admin privileges by using the pip install command:
pip install Pexpect
Once the module finished installing I opened IDLE and in it and tried importing the module:
import pexpect
which gave the error:
Traceback (most recent call last):
File "", line 1, in
import pexpect
ImportError: No module named 'pexpect'
The problem is python cant see the imported module even after its installation.How can i prevent this from happening?

This might occur if you have multiple versions of Python installed on your machine. Assuming you have Python 2.7 and 3 installed, I am guessing "pip" installed pexpect under the 2.7 libraries. Easiest way around this is to add the path to your Python 2.7 packages to your sys.path.
import sys
sys.path.append('/usr/lib/python2.7/dist-packages')
The path mentioned above tends to change depending on your Python installation. So make sure to validate the path before running your script.
Alternatively, you could use pip3 to install packages for Python 3 directly. Please refer this question for instructions.

Related

Import "ruamel.yaml" could not be resolved

I am just a beginner in python.
I have a requirement of converting a JSON Object into a YAML file and trying to import ruamel.yaml.
I did install the command pip3 install ruamel.yaml and my python list package shows that it's available.
site-packages % pip3 list
Package Version
------------------ ---------
certifi 2021.10.8
charset-normalizer 2.0.11
idna 3.3
pip 22.0.3
pytz 2021.3
PyYAML 6.0
requests 2.27.1
ruamel.yaml 0.17.21
ruamel.yaml.clib 0.2.6
setuptools 60.9.3
urllib3 1.26.8
Whereas when I am trying to import and use it in the python script it is giving me below error.
Import "ruamel.yaml" could not be resolved
Could someone please suggest how to resolve this.?
When Python cannot find a module you get an error looking like:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ruamel.yaml
This might be because you are using some non-standard installation/execution environment, or because you try to run this from an editor. Some of those don't understand the concept of namespaces in Python.
It could also be that pip3 is not installing for the python that you are using to run the program. That is easy to check by installing some other package using pip3 and trying to import that.
In either case make sure you test your program running with the python that corresponds to pip3 ( using type pip3 or which pip3 resp type python ), or install using:
python -m pip install ruamel.yaml
and then run the program with python your_prog.py

vs code can not import python package but terminal can import

I installed anaconda for python3.
I clone a package from GitHub(can not install by pip3) and set the path to python in ~/.bash_profile.
In the terminal, I can import the package successfully. when I run scripts with vs code, error info occurred: ModuleNotFoundError: No module named mypackage
I run which python3 from the terminal and got: /usr/bin/python3
I select the same python interpreter(command+shift+p) in vscode which point to the same path : /usr/bin/python3
I try to search from StackOverflow, most of the answers said terminal and vs code use the different python version, but it seems my python version is the same.
I don't have a clue and am stuck in here for a whole day. Can someone help me?

ModuleNotFoundError: No module named 'requests'. But 'requests' already installed

Trying to import 'requests'.
Has it installed via pip3 install requests? But still, have this error.
C:\Users\Vikentiy>pip3 list
Package Version
---------- ----------
certifi 2018.11.29
chardet 3.0.4
Django 2.1.7
idna 2.8
pip 19.0.2
pytz 2018.9
requests 2.21.0
setuptools 40.6.2
simplejson 3.16.0
urllib3 1.24.1
virtualenv 16.4.0
C:\Users\Vikentiy>python --version
Python 3.7.2
Error Traceback:
C:\Users\Vikentiy\untitled2\venv\Scripts\python.exe C:/Users/Vikentiy/untitled2/requeststests.py
Traceback (most recent call last):
File "C:/Users/Vikentiy/untitled2/requeststests.py", line 1, in <module> import requests`
Try to uninstall requests and then again install it using pip
pip uninstall requests
and again install it
pip install requests
or if you didn't get what I am saying so please visit
https://scmquest.com/resolved-importerror-no-module-named-requests-usr-bin-python-no-module-named-pip-on-macos/
Another cause of this might be multiple Python versions installations. So if you type in your terminal:
pip3 install requests
The requests library will be automatically installed to your default python3, for example, Python3.6. However, let's say you have another Python version, for example, Python3.7, what you should do is:
pip3.7 install requests
This should solve this problem.
If you are running your code in the terminal, you should try:
python3.7 file_to_run.py
If you're using PyCharm as IDE, try closing and restarting PyCharm. Move the mouse cursor over the "requests" (with red curly line beneath it), until the red light bulb next to it show up. Select the first option in it, "Install package requests". PyCharm will take care of the installation from there.
I ran into the same issue and have tried all the solutions here on Stack Overflow.
If it is installed this will WORK:
c:\>python (or on Mac/Linux "$ python")
>>> import requests
>>> requests.get("http://127.0.0.1")
<Response [200]>
If you see this error when running your script/IDE:
Traceback (most recent call last): File "E:\test.py", line 1, in <module>
import requests
ImportError: No module named requests
Try:
python script.py
Answer is based on https://www.edureka.co/community/84584/python-requests-module-import-error-module-named-requests
Then the trick for me was not to start up a VirtualEnv, I actually found I was running the x64 version of python when I'd installed the requests package into the python 32 bit folder c:\program files (x86)\python37-32\lib\site-packages. This is a screenshot from the internet but it shows you how to change the interpreter you're using - in my case - I need to set it to Python 3.7.4(x86) 32 bit:
Run in command prompt
and write command pip install requests in scripts directory
cd \Python27\scripts
pip install requests
That's the exact solution which works for me, please follow this:
As like, first try to get the version of the requests module installed previously, this can be find by
pip install requests
Now, you will get the message:
Requirement already satisfied: requests in /opt/anaconda3/lib/python3.8/site-packages (2.24.0)
See, here the version of the requests module i.e., (2.24.0)
Now, the simple basic logic is this like you should install just the previous version of the requests (2.24.0). So, you should now install requests (2.20.0)
For this use command:
pip install requests==2.20.0
Now, I think you can test import requests and this will work fine.
Further, if any error occurs then please let me know in the comments.
Thanks,
HaPpY Coding 🤗
This issue is due to missing request module in python package .So you can use below command to install and recheck by restarting your IDE
pip install requests
If already installed better to upgrade
pip install --upgrade pip
If you are using venv (virtual environment), you may need to consider where it's path. for my case it was outside my project folder. So I had to first install request inside the main python's path then I recreate venv inside my project.
inside_your_project#python -m venv ./venv

ImportError: cannot import name 'etree' on Python 3.6

I am getting error while running "from lxml import tree" on python3.6
>>> import lxml
>>> from lxml import etree
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'etree'
The same working on python3.4, I have tried many things to troubleshoot as below but didn't success.
python -m pip uninstall lxml
python -m pip install lxml==3.6.0
pip install -t /usr/local/lib/python3.6/dist-packages lxml==3.6.0
Just in case anybody has similar issue.
I also encountered this problem using Python3.6.
Just by uninstalling lxml and installing it again with pip the issue is resolved.
Got this working in Lambda with python 3.6
Turns out lxml wraps c libraries that are compiled for a certain processor architecture (I think)
Use precompiled binaries for lambda here: https://github.com/JFox/aws-lambda-lxml
For Windows:
After having the same problem at the instance of my Windows 2019 server, Python 3.8 and Anaconda, I downloaded the corresponding whl package, installed it with
pip install lxml-4.6.3-cp38-cp38-win_amd64
It now works without problem.
I have the same issue when deploying an Azure python function using version 3.9. I redeployed with 3.6 in Azure an everything worked fine.
Had the same while running the code in VS code.For me I got it resolved by changing the interpreter in VS code from 32-bit to 64-bit.

Raspberry Pi: How to get RPi.GIPO working with Python 3.6?

I have installed Python 3.6 using the instructions at https://gist.github.com/dschep/24aa61672a2092246eaca2824400d37f
For reasons I don't fully understand Python 3.6.5 was placed in
/home/pi/Python-3.6.5
and some modules were not available.
I installed RPi.GPIO into /home/pi/Python-3.6.5/Lib using
pip install RPi.GPIO -t .
Attempting to run a script with sudo python3.6 /home/pi/PythonProjects/omxcall_radar.py produces this error:
Traceback (most recent call last):
File "/home/pi/PythonProjects/omxcall_radar.py", line 18, in <module>
import RPi.GPIO as GPIO
ModuleNotFoundError: No module named 'RPi'
However the RPi directory is in the Lib directory
I am working at the limit of my linux skills. Can someone please explain why I am getting the error and/or what I may have done wrong in installing Python 3.6/RPi.GIPO?
There's a lot going on here.
The Python-3.6.5 directory in /home/pi is the Python source code. It's there because that's where you unpacked the Python-3.6.5.tar.xz archive you retrieved using wget. It is not where Python was installed.
When you ran sudo make altinstall, that installed Python into /usr/local (specifically, the binary itself would be /usr/local/bin/python3.6).
When you ran pip install RPi.GPIO -t ., you installed the RPi.GPIO module into your current directory, but that's not anywhere that your newly installed Python would look for it. Additionally, the pip you were using doesn't know anything about your new Python install, either.
After running make altinstall, you should probably do the following:
Install pip for your new Python install:
easy_install-3.6 pip
Use pip3.6 to install RPi.GPIO:
pip3.6 install RPi.GPIO
Finally, run your script using your new Python:
python3.6 /home/pi/PythonProjects/omxcall_radar.py
You may or may not need sudo, depending on what sort of access your script requires.

Resources