Web3.py - Unable to import 'web3' even though web3 is installed - python

I've installed web3 today via pip install web3 and since then I've been trying to import web3 into the current working file without any success.
No matter what I do, I get "Unable to import 'web3'.
Pip list finds web3 (web3 5.19.0).
Code in the file:
import web3
from web3 import Web3, HTTPProvider, TestRPCProvider
infura_url = 'URL'
web3 = Web3(Web3.HTTPProvider(infura_url))
isConnected = web3.isConnected()
blocknumber = web3.eth.blockNumber
Update
I know what's wrong, pip installs packages in C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages meanwhile python is looking for the packages in C:\Users\USER\AppData\Roaming\Python\Python39

You Don't need the first import and it is effecting you'r code cause you'r variable called web3, just delete
import web3
and it should work

pip list is showing web3 library, after figuring out I found that it is a problem of vs code python editor. Run the following code irrespective of editor error message
if run successfully
from web3 import Web3
print(Web3)
It will give output
<class 'web3.main.Web3'>
Then it mean it has some vs code python editor problem but you can ignore warning and use web3 for coding purpose

You Don't need the first import and it is effecting you're code cause you're variable called web3, just delete
import web3
and for webSocket use it:
Web3(Web3.WebsocketProvider(infura_url))
and for http use it:
Web3(Web3.HTTPProvider(infura_url))

This is a problem with Visual Studio Code.
I tried to open the code with PyCharm and it is working properly.

you must install visual c ++ build tools // desktop development with c ++ // then you install web3 in the console and it will not give you an error
https://visualstudio.microsoft.com/visual-cpp-build-tools/

This sounds like an issue with your local version. I would try quitting the app and try again.

Some suggested that it works despite of the error. But in my case it wasn't. So, the solution for me was installing web3 globally on my local system, outside of the virtual env using pip3 install web3 command. I'm using python3 on Ubuntu 20.04 LTS.

delete first line of code.
also delete import HTTPProvider or use code below
web3 = Web3(HTTPProvider(infura_url))
instead of
web3 = Web3(Web3.HTTPProvider(infura_url))

you should set up a virtual environment to avoid the type of issue you just described (packages being installed in different locations on your machine)
check this out for more info:
https://docs.python.org/3/library/venv.html
heres my quick guide
open up a terminal, navigate to the root directory of your project and set up a virtual environment
python3 -m venv /path/to/new/virtual/environment
to activate your virtual environment run the following command
source /path/to/new/virtual/environment/bin/activate
now run pip install web3
now that your virtual environment is activated and you've installed python3 you should be able to run your file
python name_of_file.py

I had the same situation.
The solution is to rename the python file from Web3.py to something else.

Related

Python Firebase (Real-time database) on Replit

I want to connect to firebase on replit using python. I'm using the code below:
from firebase import firebase
firebase = firebase.FirebaseApplication("my firebase url", None)
data = {
'ddfjaklfbja': 'dklabja;kdlf',
'adfjlkgadjf': 'dkaljbaowq',
'afbiouadofpia': 'dsajklbjanzm'
}
result = firebase.post('/test1', data)
print(result)
However, when I try to run it, it returns an error saying that there is no module named urllib3:
ModuleNotFoundError: No module named 'urllib3'
When I try to use the packages tab to install it on the left, it does not install and returns with the same error (there are multiple lines but the last line says this). How can I fix it?
Their are multiple things you can try:
Open your command prompt and enter "pip install urllib3"
If it can't install it try this: python3 -m pip install urllib3 or pip3 install urllib3
Perhaps it helps when you add this to your python file: import urllib3
this issue is related to Python path, and with the python interpreter. Make sure you are using the python interpreter in which you have installed the modules. (If using 3.9 use python 3.9 as interpreter as opposed to 3.10). If this doesnt work check python path. Other easy solution is to change your project folder to one that imported your modules correctly.

"unresolved import" in Python - all other imports worked so far?

I am a bit lost. I've just recently started working with python and have been able to use other libraries, that I've imported without any issues.
Install via pip command
Restart Visual Studio Community 2019
I can use the library
Now I wanted to import the docx2pdf library. But using the same 3 steps is not working and I don't know what to do.
from docx2pdf import convert <-- gets the error "unresolved import"
Also:
after installing it via pip I get the following message in addition to the installation being a success:
WARNING: The script docx2pdf.exe is installed in 'C:\Users\user\AppData\Roaming\Python\Python37\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
I didn't get this message from other libraries.
I am using windows 10.
Thanks for the help!
In VS Code, when we use the global python environment to install the module, we can use the command "pip show docx2pdf" to check its storage location is: "\users\username\appdata\local\programs\python\python38\lib\ site-packages"
It is recommended that you reinstall the module "docx2pdf" or try to delete the previously installed "docx2pdf" folder and reinstall it.
Run:

how can I install zmq in python pycharm and fix the error

I am new to Python and Pycharm and in my project (Python 2.7) I am trying to launch some code which uses zmq as a dependency. I installed zmq through package manager and added manually some paths to env. Currently it looks like the following:
But when I run the script in fails with the following message: from . import
(constants, error, message, context,
ImportError: DLL load failed: Не найден указанный модуль.
So it looks like this:
so basically it is the first line of code which generates this error:
import os, sys, time, signal, zmq
Any ideas how to fix it would be welcome. Thank you
I suppose I have Cython as when I try to install it I see the following:
pyzmq is also present:
issue solved.
just installed pure Python 2.7 interpreter from https://www.python.org/ and added it as an interpreter in PyCharm, Also installed protobuf and pyzmq there and it worked. I think it was an Anaconda issue, although I removed zmq module from there leaving only pyzmq.

Module Import Error , how to refresh Ipython session?

I am a total newbie to python. I am using docker as my virtual environment.
I am trying to run this line of code on ipython
pivot_df.to_excel(os.path.expandvars('/home/user/code.xlsx'))
and I am getting the following error:
ImportError: No module named openpyxl
I installed openpyxl successfully and then tried to import openpyxl again on ipython but with no success.
Will I have to refresh my ipython session for the openpyxl to work? If yes, how do I do that? Will I lose everything I ran until now if I do that?
I don't think you need to reload as such, so import should work after you installed a package.
It may be related to some python path of virtual environment issue. Where you installed package in one python and ipython is running in another configuration.
Best thing is to execute ! pip install openpyxl from ipython itself. This will make sure you install the package in correct environment. Then it should work

How to solve "Unresolved import: HTML" in Python Development?

I'm starting to learn about python development in a new project.
I got setup almost everything right, but only this import HTML that keeps given me some error that I don't know how to solve it.
import web
import json
from WebServer.forms import mainPageForm, addBugForm, addProblemForm, addProblemTypeForm, versionsDropdownForm,\
severitiesDropdownForm, problemTypesDropdownForm, problemsDropdownForm
import BugRecorderCore.controller as ctrl
import BugRecorderCore.validators as vdt
import datetime
import os
from BugRecorderCore.utils import concatenateString
import HTML
//...
I already tried to install HTML.py already, but still no success so far.
Any idea or advice about this issue ?
UPDATE
Following the suggestions from the answers below I got this message:
It looks like you are using anaconda, have you tried installing it the anaconda way?
conda install HTML
Also do you by any chance have 2 version of Python on your system?
If the package is unavailable you'll have to user pip. If you don't have pip, from your command line write:
python get-pip.py
pip install HTML
Looking the given screenshots and tags I suppose your are using Anaconda (which I have no experience but it is still Python, anyway) and the IDE is not resolving the import.
Make sure you have installed/updated HTML.py
conda install HTML
At your IDE go to Window > Preferences > Python Interpreter
At Libraries tab make sure you have the following folders added to your PYTHONPATH:
C:\Anaconda\Lib
C:\Anaconda\Lib\site-packages
C:\Anaconda\Scripts
That should do the trick.
Important: try to always install your libraries through conda (or pip when using Python directly). It will install things where it should be. ;)

Categories