Python VS Code imports not working 'No module name requests' - python

I'm new to python, coming from 6 months of c++ and am also new to VS Code (from VS).
My error is>
Exception has occurred: ModuleNotFoundError
No module named 'requests'
File "C:\Users\ryanb\Documents\Python\main.py", line 1, in
import requests
on anything which begins with :
import requests
this works fine in VS, but I have been encouraged to use Code. When I ran the pip command it said 'satisified'.
PS C:\Users\ryanb\Documents\Python> pip install requests
Requirement already satisfied: requests in c:\users\ryanb\anaconda3\lib\site-packages (2.22.0)
Requirement already satisfied: idna<2.9,>=2.5 in c:\users\ryanb\anaconda3\lib\site-packages (from requests) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\ryanb\anaconda3\lib\site-packages (from requests) (1.25.8)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\ryanb\anaconda3\lib\site-packages (from requests) (2019.11.28)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\ryanb\anaconda3\lib\site-packages (from requests) (3.0.4)
PS C:\Users\ryanb\Documents\Python>
I have anaconda, though I'm new to it as well, but I seem to get the problem either way.
Initially my problem was that it wouldn't find the interpreter but this was resolved after I installed add on files to visual studio (not visual studio code) and used the interpreter which came with that.
Please advise.
Thank you

You may have several python versions installed (e.g. 3.6 32-bit, 3.6 64-bit, 2.7, ...). I would recommend to "natively" install Python from https://www.python.org/ instead of using Anaconda. The problem can happen because VS code is using another Python, different than the one you installed requests. You can verify this if pip3 is installed: try pip3 --version. Anyway, you can change the interpreter VS code is using by clicking the lower bar: screenshot
Or you might just run pip3 install requests.
I would recommend you to leave just one Python installed in your machine.

Related

Unable to import pandas in Replit.com

import pandas
So I am working on a python project and want to import pandas library on https://replit.com/~
For some reasons, it shows some attribute error when I run the project.
Does anyone know ho do I fix or manually install pandas on replit?
Attaching Screenshot of an error herewith.
Usually packages have a lot of errors in replit but you can try this: -
Pandas does actually work on repl.it - you have to install it from the package manager. To do so, click the cube on the side navigation bar and type pandas into the search box. Then click on the pandas search entry and hit the plus sign. Tell me if this works!
Or
Broken package installs can usually be fixed by,
Updating pip and installing pandas from PyPI. By default, Repl.it comes with pip version 19.3.1, but the latest available version for python 3.8 is pip-21.1.1.
~/repl$ pip -V
pip 19.3.1 from /opt/virtualenvs/python3/lib/python3.8/site-packages/pip (python 3.8)
~/repl$ pip install pandas
Requirement already satisfied: numpy>=1.16.5 in /opt/virtualenvs/python3/lib/python3.8/site-packages (from pandas) (1.20.2)
Collecting pytz>=2017.3
Using cached https://files.pythonhosted.org/packages/70/94/784178ca5dd892a98f113cdd923372024dc04b8d40abe77ca76b5fb90ca6/pytz-2021.1-py2.py3-none-any.whl
Requirement already satisfied: python-dateutil>=2.7.3 in /opt/virtualenvs/python3/lib/python3.8/site-packages (from pandas) (2.8.1)
Requirement already satisfied: six>=1.5 in /opt/virtualenvs/python3/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas) (1.15.0)
Installing collected packages: pytz, pandas
Successfully installed pandas-1.2.4 pytz-2021.1
Pandas does not work on replit at all, because a lot of modules, which Pandas needs to work properly, do not work in replit. An online Compiler is also not the best choice for doing dataprocessing, it would be better if you install an Interpreter for python on your PC.

How do I fix this error in Spyder: No module named 'flask'

Updated:
So after doing some digging I was able to find that if I add a '!' before pip it allows me to run pip inside of Spyder. (I was unable to run pip inside the console originally. That is why below you will see I ran a version check on flask in a python cmd
Here I'm trying to install the latest version of flask just to make sure I have it installed.
If I run !pip install Flask I get the following:
Requirement already satisfied: Flask in {path}\python37-32\lib\site-packages (2.0.1)
Requirement already satisfied: Werkzeug>=2.0 in {path}\python37-32\lib\site-packages (from Flask) (2.0.1)
Requirement already satisfied: Jinja2>=3.0 in {path}\spyder\pkgs (from Flask) (3.0.0)
Requirement already satisfied: itsdangerous>=2.0 in {path}\python37-32\lib\site-packages (from Flask) (2.0.1)
Requirement already satisfied: click>=7.1.2 in {path}\spyder\pkgs (from Flask) (8.0.0)
Requirement already satisfied: colorama in {path}\spyder\pkgs (from click>=7.1.2->Flask) (0.4.4)
Requirement already satisfied: MarkupSafe>=2.0.0rc2 in {path}\spyder\pkgs (from Jinja2>=3.0->Flask) (2.0.0)
That means that flask is definitely installed and can be reached by Spyder. When I run the file the following command is run:
runfile('{path_to_folder}/{file_name}.py', wdir='{path_to_folder}')
I still don't know what is wrong with this, and I don't use Spyder very often so I don't believe it could be an environment variable that I messed with causing the issue.
Original Post:
So the issue is when I run my code in spyder I get the error below
ModuleNotFoundError: No module named 'flask'
I'm importing flask at the top of my program. Below is what I get when I run my .py file in spyder.
from flask import Flask, jsonify
I checked to make sure I have flask installed and I do. I ran python in cmd to check which version of flask I was runnning.
>>> import flask
>>> flask.__version__
'2.0.1'
I'm not sure what else could be the issue. I'm not using a virtual environment so that isn't causing an issue.
Problem
Here was the issue.
From what I can tell it's related to a known issue in Spyder 5. When I started the project that I'm currently working on I updated to the latest version of Spyder. Apparently, there is an issue with some of the dependences in Spyder 5 and that was causing the issue.
You can read more about the exact issue here
Solution
I simply downgraded to Spyder 4 until the patch for this issue is released.

Why can't python find my installed libaries?

I use python 3.9 and it is no longer able to find the libraries that I have installed libaries even though they are installed.
When I for example import pandas I get this message,
>>> import pandas
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pandas
ModuleNotFoundError: No module named 'pandas'
When I use pip to install the library it tells me that it already has been installed.
C:\Users\malth>pip install pandas
Requirement already satisfied: pandas in c:\users\malth\appdata\local\programs\python\python39\lib\site-packages (1.2.3)
Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\malth\appdata\local\programs\python\python39\lib\site-packages (from pandas) (2.8.1)
Requirement already satisfied: pytz>=2017.3 in c:\users\malth\appdata\local\programs\python\python39\lib\site-packages (from pandas) (2021.1)
Requirement already satisfied: numpy>=1.16.5 in c:\users\malth\appdata\local\programs\python\python39\lib\site-packages (from pandas) (1.20.1)
Requirement already satisfied: six>=1.5 in c:\users\malth\appdata\local\programs\python\python39\lib\site-packages (from python-dateutil>=2.7.3->pandas) (1.15.0)
I can even see the library in the site-packages folder.
Try creating virtual environment, configure it and then install whatever module you require. Its the most easiest way to skip module error anytime.
Try to do pip --version. If you have a multiple pythons on your pc, you might have a pip that linked to another version.
I guess you should try to use pyenv or virtualenv. This modules allowed you to avoid situation which I described above. Also it's a good practice to keep you python env isolated for a specific project.
As N.Singh has suggested, I would try to set up a new conda environment, adding all possible packages or libraries you will use and then I will make it active. Information about it can be found here: https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#activating-an-environment.
I really hope this helps. I have already had the same problem few months ago and worked for me.

Why don't my modules install in my current python environment?

I have a recurrent problem I have yet been unable to find an answer to.
Whenever I install a package in PyCharm through the terminal, whenever I try importing the module the package appears to not be installed. Or, like just now, the package may already be installed but when importing the module I get the error "No module named x":
C:\Users\TimStack\PycharmProjects\API>pip install requests
Requirement already satisfied: requests in c:\users\TimStack\appdata\local\programs\python\python37\lib\site-packages (2.23.0)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\TimStack\appdata\local\programs\python\python37\lib\site-packages (from requests) (2019.11.28)
Requirement already satisfied: idna<3,>=2.5 in c:\users\TimStack\appdata\local\programs\python\python37\lib\site-packages (from requests) (2.9)
Requirement already satisfied: chardet<4,>=3.0.2 in c:\users\TimStack\appdata\local\programs\python\python37\lib\site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\TimStack\appdata\local\programs\python\python37\lib\site-packages (from requests) (1.25.8)
Looking at these directories, it seems to refer to an old Python 3.7 installation. However, my environment uses 3.8.
What's the issue at hand here, and how do I go about solving it?
You need to uninstall the old 3.7 version as you have 2 conflicting Python versions.
OR
Specify your Python version when installing packages:
pip3.8 install [package name]
(3.8 for Python 3.8)
pip install takes --python-version <python_version> as argument.
You can pass 3.8 as argument and it will pick up the right one for you.
from the docs -
The Python interpreter version to use for wheel and "Requires-
Python" compatibility checks. Defaults to a version derived from
the running interpreter. The version can be specified using up
to three dot-separated integers (e.g. "3" for 3.0.0, "3.7" for
3.7.0, or "3.7.3"). A major-minor version can also be given as a
string without dots (e.g. "37" for 3.7.0).

installing tweepy on Mac using pip - error message

I have recently switched laptops from Microsoft to Apple (2015 MacBook Pro) and I have been installing python libraries such as Nlkt succesfully without any issues.
Now I have been trying to install tweepy and seem to get loads of different error messages.
After trying I pip install tweepy[error message saying "invalid syntax" in my terminal] I did some research on here and tried the below solution:
import pip
>>> package_name='tweepy'
>>> pip.main(['install',package_name])
OSError: [Errno 1] Operation not permitted: '/var/folders/t1/4g62trws5812jb97vvw5kp900000gn/T/pip-yabtGc-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'
2
>>>
As you can see it is still not working...
I have also tried sudo pip install tweepy, which came up with this message:
The directory '/Users/MYNAME/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: tweepy in ./Library/Python/2.7/lib/python/site-packages
Requirement already satisfied: six>=1.7.3 in ./Library/Python/2.7/lib/python/site-packages (from tweepy)
Requirement already satisfied: requests-oauthlib>=0.4.1 in ./Library/Python/2.7/lib/python/site-packages (from tweepy)
Requirement already satisfied: requests>=2.4.3 in ./Library/Python/2.7/lib/python/site-packages (from tweepy)
Requirement already satisfied: oauthlib>=0.6.2 in ./Library/Python/2.7/lib/python/site-packages (from requests-oauthlib>=0.4.1->tweepy)
Requirement already satisfied: urllib3<1.22,>=1.21.1 in ./Library/Python/2.7/lib/python/site-packages (from requests>=2.4.3->tweepy)
Requirement already satisfied: idna<2.6,>=2.5 in ./Library/Python/2.7/lib/python/site-packages (from requests>=2.4.3->tweepy)
Requirement already satisfied: certifi>=2017.4.17 in ./Library/Python/2.7/lib/python/site-packages (from requests>=2.4.3->tweepy)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in ./Library/Python/2.7/lib/python/site-packages (from requests>=2.4.3->tweepy)
When I tried to import tweepy into idle it said "no module named tweepy"
Sorry for my longwinded explanation but I'm getting desperate...
Thank you very much for your help already!!
One solution is to use virtualenv.
In short, virtualenv creates a separate python instance in a folder different from the computers' python installation. This allows for easy installation of packages and helps you manage dependencies better than installing everything globally with pip.
How do I do this?
First, install virtualenv
$ pip install virtualenv
Next, create a virtual environment. The following command will create a python instance in a folder named "foo" in the directory you execute this from.
$ virtualenv foo
Now, whenever you need to install something, use the pip that is located at foo/bin/pip and the python located at foo/bin/python.
$ foo/bin/pip install tweepy
You can test this via the python interpreter:
$ foo/bin/python
Python 2.7.10 (default, Feb 6 2017, 23:53:20)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tweepy
Why would you want to do this?
If you use virtualenv, you are less likely to run into dependency issues.
For example, lets say you make some python program, called ProjectAAA, which uses version 1.1 of LibraryXYZ. A couple months later you want to write a new, completely unrelated python program, ProjectZZZ, which uses version 2.2.5 of that same library. When LibraryXYZ upgraded from 1.1 to 2.2.5, they removed and renamed a few classes and functions that you used in ProjectAAA. So, if you used pip and installed to the main python instance on your Mac, you won't be able to run ProjectAAA anymore, because it depends on version 1.1 but your computer has version 2.2.5.
If you used virtualenv, you could have two separate python instances, each with their own pip and their own package installations. So the environment for ProjectAAA has version 1.1, the environment for ProjectZZZ has version 2.2.5, and both can live peacefully on your computer.
I use OS X and I just successfully installed tweepy on Python IDLE using:
import pip
package_name='tweepy'
pip.main(['install',package_name])
It was pretty easy.
Thanks for the provision; now I can get on with my first extraction.
From looking at your error message, it appears you already have it.
Requirement already satisfied: tweepy in ./Library/Python/2.7/lib/python/site-packages
I tried replicating your issue (in both Python 2.7/3.5) and didn't run in to any issues and was able to import it in to IDLE.
A potential solution could be changing your PATH directory to the one that's listed in your error message.
export PYTHONPATH=$PYTHONPATH:./Library/Python/2.7/lib/python/site-packages
Another thing would be to consider using a virtual environment in the future to ensure that there is no conflict in your package installations.
Further notes: Installing Python on Mac OS X: virtualenv

Categories