Python - import requests ImportError: No module named requests - python

I have installed python 2.7 and also I have requests package installed. I am not sure of the reason, the site-packages are in a different location and my python 2.7 core files are in a different location
C:\Program Files\Python27
C:\Users\MyID\AppData\Roaming\Python\Python27\site-packages
I can see requests and the binaries in the path C:\Users\MyID\AppData\Roaming\Python\Python27\site-packages\requests. Yet when my job runs, it throws an error as shown below.
File "C:\Program Files (x86)\Jenkins\workspace\code\xxxx.py", line 36, in <module>
import requests
ImportError: No module named requests
I have only one version of python installed.
C:\Users\MyID\Desktop\xxx\Automation\xxxxxx\venv\Scripts>python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

This is most probably the problem of virtual environment, virtual environment (venv) isolate your work environment in such a way that you can have different workspace for same version of python, that's why you are getting the error, I suspect that you installed request module in original python, but trying to run the script in virtual environment, hence it's giving error
Here, try these steps and tell the result
Open CMD
Write cd C:\Users\MyID\Desktop\xxx\Automation\xxxxxx\venv\Scripts , Press enter.
Write python -m pip install requests
Wait for it and tell the result, this must fix your problem
For reference on virtual environment and pip, look at this.

Related

Python 3.6.x + Windows: Interpreter just shuts down for any external library

Due to external dependencies I need to run python 3.6.x (I know it is EOL) on Windows.
However, any external library causes the interpreter to just silently quit.
As an example I have installed numpy in the following way
py -m pip install numpy
And then running Python through PowerShell:
PS C:\Users\USER> py
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
PS C:\Users\USER>
As soon as I run import numpy the python interpreter simply silently returns.
I have tried to install every python 3.6.x available from Pythons official website, and the issue persists.
I fully removed old versions of python between each install.
I cannot find any error logs or anything.
What can be the issue here and how can I debug further?
Any internal library (that I have tried) works
I have tried with the following libraries:
Numpy
Tensorflow
Protobuf
matplotlib
scipy
pandas
nose
sympy

I don´t know what version of Python I have, and problem coding my first website

I have installed the latest version of python (3.7.3)
When I go to cmd and I put py, it says my version is 3.7.3. But the problem comes when i put python or python --version, because it says that my version is 2.7.10. And i don´t know why.
I´m learning how to do my first website, and i´m just following what the tutorial says. I don´t know if the version of python is the cause of the problem, but i tell you what happens to me appart from not knowing what version i have.
I´m using Visual Studio Code and the guy from the tutorial said that we had to have at least a version of python over 3, and to find this out we have to write on CMD python --version. Then we had to know what version of pip we have putting pip --version.
Now I´ll show you the code he did and I copied, I had to install flask from CMD putting pip install flask.
So when I go to CMD to run this as the youtuber did, putting cd Desktop then cd NoFear (NoFear is the name of the folder) and finally python index.py. I got the following output:
C:\Users\Usuario\Desktop\NoFear>python index.py
Traceback (most recent call last):
File "index.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
C:\Users\Usuario>py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\Usuario>python
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\Usuario>python --version
Python 2.7.10
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return 'Hello World'
if __name__=='__main__':
app.run()
Type Py -3 index.py in cmd instead of typing python index.py. I hope this helps
You have two versions of python so uninstall the older version first
You can create a virtual environment that will isolate your Python dependencies. Because you have stated you are using Python 3.7.3, you can create a virtual environment by running the following command:
$ python3 -m venv .venv
Then you can activate it by using:
$ source .venv/bin/activate
Then if you run python in the terminal, you will start Python 3.7.3 because your virtual environment has been created using this version of Python.
Finally, if you want to deactivate the virtual environment just run:
$ deactivate
You can uninstall a your other python versions or if you want to keep them you can rearrange the path. That being said I would recommend looking into creating virtual environments.
1) In the bottom left of windows search for: environment variables
2) Select Environment Variables... on the bottom right.
3) Select the Path variable on the top (or bottom if there is not one on the top).
4) Move python3 above python2.7 and save.
Maybe get atom cause you are working in IDLE and i had the same problem each time you want to use code form IDLE on other software you cannot run it because of the lines on top
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
you have to take them of or use virtual studio or atom or oder IDE, i think.

Anaconda installed but No module named 'conda' after installing flake8

While trying to install atom-lint package in Atom editor I somehow corrupted my conda installation.
I did the following things that might have caused the issue:
Installed a python dependency Flake8 using conda install.
Messed around with Atom Init Script (I can provide more info if needed)
After I did these things I encountered the following problem:
If I run conda I get
$ conda
Traceback (most recent call last):
File "/Users/me/miniconda3/bin/conda", line 12, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
Strangely enough if I run python in my command line anaconda still seems to be installed an working.
$ python
Python 3.7.1 (default, Oct 23 2018, 14:07:42)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
I have also noticed that the Python3 bin has disappeared from the conda environment folder ~/miniconda3/envs/my_env/bin/
This is very strange and I thought might be related.
Any help would be much appreciated.
I ended up making a backup copy of the miniconda3/envs folder, reinstalling miniconda and copying the environment back in. It works now, not sure what caused the issue.

'import pyodbc' leads to 'No module named pyodbc '

I recently ran pip install pyodbc. Which says it installed pyodbc successfully on the cmd.
However when I do import pyodbc in IDLE I receive an error saying 'No module named pyodbc'.
It seems only two files have been installed when I ran pip install pyodbc.
The two files:
pyodbc-4.0.26.dist-info
pyodbc.cp36-win32.pyd
VERSION of Python:
Python 3.6.3
Complete Error Message:
Error Message
How do I resolve this issue? Any help would be much appreciated, I'm new to python and this is quite frustrating.
The files that installed are fine. *.pyd is compiled library (see https://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll).
Issues that may cause this:
IDLE running main python installation and you installed the package in virtual environment
You have several installations of python e.g. python 3.x alongside python 2.x
I think first of all try to run all the steps from the same cmd:
PS C:\Windows\system32> pip install pyodbc
Collecting pyodbc
Using cached https://files.pythonhosted.org/packages/17/00/7115c072d4d01da4feee740cf5d964b4367ba0f9843d334d64ef77fd2baa/pyodbc-4.0.26-cp36-cp36m-win_amd64.whl
Installing collected packages: pyodbc
Successfully installed pyodbc-4.0.26
# now just run python interpreter
PS C:\Windows\system32> python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyodbc
# no error
This should work...
And then try to understand if you have several virtualenvs or python instalations.
You can check which python IDLE run, by right click on it's shortcut and "open file location" for example

Python 2.7.9: No module named requests

I am writing a code in Python 2.7.9 for which I need the requests module. I installed the module using sudo pip install requests but still in python 2.7.9 I am getting an error as follows:
Python 2.7.9 (default, Jan 5 2016, 18:47:14)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
I checked the installation location /usr/local/lib/python2.7/dist-packages, it contains the requests package directory. The same thing works fine in python3, so I am guessing it's an installation error. How can I fix it ?
EDIT:
On executing pip lists, I could see requests (2.1.9) in the list. After I ran pip uninstall requests it shows requests (2.2.1) in pip list.
Based on the comments, it seems you have installed python 2.7.9 using a method Ubuntu doesn't like. Because of this the dist-packages folder is not added to your sys.path. You could set the PYTHONPATH variable in your .bashrc (or other zshrc, etc) to add that folder to your sys.path by default.
Better method would be to use a library like pyenv (It handles all dependency issues flawlessly for multiple python versions) or a better supported ppa for the latest python where this problem shouldn't arise at all.
Also, you have 2 versions of requests. This seems to be because one is installed using apt-get (sudo apt-get install python-requests) and the other is from pip (sudo pip install requests). It would be good to remove one of them to avoid confusion.

Categories