failed to use urllib.request with python in vscode - python

I tried to import urllib.request wth python in vscode but kept getting the message: ImportError: No module named request. I have no problem importing just urllib, which I know is the right line for python2. I'm sure I'm using python3 as that's what I've selected in the interpreter path.
Here's the screenshot:
I've also put pip3 install urllib3 in the terminal and got this message which means python3 should have it:
Requirement already satisfied: urllib3 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (1.26.9)
any idea what have gone wrong or it's not the problem with python version?

This is related to your multiple environments. You should specify the installation path when using pip installation package.
pip install -t flodername urllib3 #the package of the python interpreter you chosen

Related

ImportError: No module named requests but module already exists

My code fails when it tries to import requests, despite it already being installed. I did a pip list and saw the module requested there, I uninstalled it and reinstalled it with both pip install and pip3 install, also added sudo both times. Whenever I try to install it I get the message that the requirement is already satisfied. Is there something else that I could try?
If it helps I am using VSCode on a Mac, I also have Jupyter and Spyder installed and have used them before however I’ve never used the requests module on this device.
UPDATE:
I created a virtualenv and installed requests there, when running the script in the venv I am not getting the error anymore, however I am still curious why it was being thrown on the base env, anything else I could check?
You probably have multiple installations/environments.
Before the "import requests", line put "import sys; print(sys.executable)".
This prints the python executable being used - verify that it is the same one that you can successfully import requests with .
What worked for me was deleting three folders having names starting with "request-SOMETHING" in the directory that pip3 specifies when you try to install requests again, e.g.
Requirement already satisfied: requests in /usr/lib/python3/dist-packages
Then just reinstall with pip and it should be in your sys.executable directory.
Try this
pip install chardet2 urllib3
or
python3 -m pip install requests
There is a problem with package dependency

import bs4 ModuleNotFoundError: No module named 'bs4'

I am trying to create a web script, but when I run it it gives me this error:
import bs4 ModuleNotFoundError: No module named 'bs4'
When I go to my project interpreter, bs4 and beautifulsoup4 are installed. Also, when I run the command pip install beautifulsoup4, it says that:
Requirement already satisfied: soupsieve>1.2; python_version >= "3.0" in ./venv/lib/python3.8/site-packages (from beautifulsoup4) (2.1)
My Python version is 3.8.0.
Try to run this command in the same interpreter as your python script.
pip install beautifulsoup4
You may have more than one interpreter setup on your machine, and when you type the command it does not install to the interpreter/virtual environment where your python script is running.
Solution
Follow the following steps in order, if applicable to your situation:
If you are using an IDE, such as Pycharm or VSCode, you will have an option to choose your virtual environment. Keep note of the name of the environment.
Then, activate your virtual environment, and install the package using the command I specified above. This process can be done from the command line or IDE
Run your python script in that environment/interpreter
This should have liberally answered the question, but if you want to learn more about virtual environments and interpreters, then use this link.
https://realpython.com/python-virtual-environments-a-primer/
I use PyCharm Python3.7 , is ok.
Python3.8 should be fine.
pip3 install beautifulsoup4
from bs4 import BeautifulSoup

ModuleNotFoundError using pip

I have installed the module alpha_vantage using pip. In CMD writing import alpha_vantage does not show an error message, so I assume it is installed properly.
However, when trying to import alpha_vantage in a python program I get this error: "ModuleNotFoundError: No module named 'requests'". I assume the issue is due to the location of the module.
The project is located here:
C:\Users\M\Documents\Python\Investering.
I tried placing the module here by pip: C:\Users\M\Documents\Python\Investering\modules\alpha_vantage
Note pip did not install the module there I simply copied the module folder and placed them in the project folder.
Is it possible to simply copy and paste module folders in other projects? Or how am I supposed to make python acknowledge the module?
The module was installed here:
C:\Users\M\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\alpha_vantage
I'm quite new to python. I have tried adding to sys.path the folder where the module was installed, but it did not seem to help.
Thanks for any help.
Edit: I tried pip install requests and it produced this:
Requirement already satisfied: requests in c:\users\martin\appdata\local\programs\python\python38-32\lib\site-packages
Someone mentioned running: "pip install foo"
It, however, produces this error message:
ERROR: Could not find a version that satisfies the requirement foo (from versions: none) ERROR: No matching distribution found for foo
I am using PyCharm.
I found my error. I had installed python from the website, but had unknowningly installed the 32 bit version, and a 64 bit version. I uninstalled all my python versions and pycharm. Then I reinstalled everything. Now I could easily make the module work. Thanks for the pointers.

Installing python package

I've already installed mechanize using pip install mechanize, such that when I enter it in the command prompt, the response is: Requirement already satisfied: mechanize in c:\users\wwl\anaconda3\lib\site-packages.
However, when I type import mechanize in the jupyter notebook, it gives an error:
ImportError: No module named '_mechanize'.
Where have I gone wrong?
Are you using Virtualenv?
If so, make sure you are importing the global libraries to your virtualenv (https://virtualenv.pypa.io/en/stable/userguide/#removing-an-environment).
If not, probably your Python version is not compatible. https://stackoverflow.com/a/24001585/2051397
Hope it helps

ImportError: No module named 'bs4' with virtualenv python3

I know this is a common issue but I still can't figure how to make it work.
I'm trying to use bs4 inside a virtual environment but I have this error ImportError: No module named 'bs4'
I have my virtualenv activated :
pip install bs4
Requirement already satisfied: bs4 in /home/****/.virtualenvs/****/lib/python3.5/dist-packages
Requirement already satisfied: beautifulsoup4 in /home/****/.virtualenvs/****/lib/python3.5/dist-packages (from bs4)
My scripts start with
!/usr/bin/python3.5
# -*-coding:Utf-8 -*
import json
import hashlib
from bs4 import BeautifulSoup
Any help is welcome !
Are you sure you are using the right python? If you have everything tied up in a virtual environment, then you need to be using python from inside that virtualenv. The command:
which python
at a bash prompt should tell you what the command:
python
points to.
From the look of your script, you are asking it to go to /usr/bin/python3.5, which is not the directory of your virtualenv, by looking at your path, it is somewhere in your home directory. Try changing the path in your script to the python virtualenv version (the one which pip has found bs4 in).

Categories

Resources