How to check the version of urllib - python-3.x

I created a python virtual env. As urllib is a standard library, I am able to import it directly using import urllib. I want to find out what version of urllib is installed. I tried checking in site-packages but could not find the library there. I tried doing urllib.__version after import urllib. But it's throwing the error AttributeError: module 'urllib' has no attribute '__version__'. How can I find the urllib version?

In python2.7, you can simply get:
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05)
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.__version__
'1.17'
>>>
In Python3, urllib was separated to multiple packages, so you need to get the version from urllib.request:
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40)
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.__version__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'urllib' has no attribute '__version__'
>>> import urllib.request
>>> urllib.request.__version__
'3.9'

Even if you try to install it , it will show the output as - which shows it is already installed
root#lancer:# pip install urllib3
Requirement already satisfied: urllib3 in /usr/local/lib/python3.6/dist-packages (1.26.2)

Related

Why will SQLAlchemy import in Jupyter Notebook, but not in a virtual environment?

I keep getting a ModuleNotFoundError when trying to import SQLAlchemy in the interpreter.
% pip list
Package Version
---------- -------
greenlet 2.0.1
pip 22.3.1
setuptools 65.6.3
SQLAlchemy 1.4.45
wheel 0.38.4
Python 3.9.6 (default, Oct 18 2022, 12:41:40)
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import SQLAlchemy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'SQLAlchemy'
>>> import sqlalchemy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'sqlalchemy'
I tried uninstalling SQLAlchemy, deactivating the virtual environment, reactivating the virtual environment, and upgrading pip. This did nothing. I still have the same error.
As I understand it for historical reasons the distribution name, ie. on PyPi, is not always the same as the import name. Confusing but the way it is. This doesn't just affect SQLAlchemy but a lot of libraries have this issue.
$ ve/bin/pip install SQLAlchemy
Requirement already satisfied: SQLAlchemy in ./ve/lib/python3.10/site-packages (1.4.43)
Requirement already satisfied: greenlet!=0.4.17 in ./ve/lib/python3.10/site-packages (from SQLAlchemy) (2.0.0.post0)
Python 3.10.3 (main, Apr 22 2022, 11:37:53) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import SQLAlchemy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'SQLAlchemy'
>>> import sqlalchemy
>>>
It's case sensitive just use sqlalchemywith lowercase.
import sqlalchemy
Tutorial with basic use:
https://towardsdatascience.com/sqlalchemy-python-tutorial-79a577141a91

ModuleNotFoundError: No module named <module_name>

Looks these two modules are installed:
ganningxu#Gannings-iMac:~$ pip3 list |grep -i ezgmail
EZGmail 2020.9.29
ganningxu#Gannings-iMac:~$ pip3 list |grep -i twilio
twilio 6.35.5
But I can't get them imported in python3:
ganningxu#Gannings-iMac:~/python_prog/texting$ python3
Python 3.9.0 (default, Nov 21 2020, 14:55:42)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ezgmail
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'ezgmail'
>>> import twilio
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'twilio'
>>>
What could be wrong with my python3 environment to cause this?
Update:
Looks like ezgmail was installed into /Library/Python/3.7/site-packages.
Yet, it looks the above path was not among those in sys.path.
ganningxu#Gannings-iMac:/Library/Python/3.7/site-packages$ python3
Python 3.9.0 (default, Nov 21 2020, 14:55:42)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/local/Cellar/python#3.9/3.9.0_2/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/usr/local/Cellar/python#3.9/3.9.0_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/usr/local/Cellar/python#3.9/3.9.0_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/site-packages']
Also, the confusing part is some libs were installed into /Library/Python/3.7/site-packages, as:
ganningxu#Gannings-iMac:/Library/Python/3.7/site-packages$ ls
EZGmail-2020.9.29.dist-info filelock.py
__pycache__ importlib_metadata
_distutils_hack importlib_metadata-1.5.0.dist-info
appdirs-1.4.3.dist-info pkg_resources
appdirs.py setuptools
distlib setuptools-50.3.2.dist-info
distlib-0.3.0.dist-info wheel
distutils-precedence.pth wheel-0.35.1.dist-info
easy_install.py zipp-3.0.0.dist-info
ezgmail zipp.py
filelock-3.0.12.dist-info
But some others were installed into the similar path in my homedir:
ganningxu#Gannings-iMac:~/Library/Python/3.7/lib/python/site-packages$ ls
Django-2.1.5.dist-info multidict
IMAPClient-2.1.0.dist-info multidict-4.7.5.dist-info
PIL nose
Pillow-8.0.0.dist-info nose-1.3.7.dist-info
Pillow-8.0.1.dist-info oauth2client
PyJWT-1.7.1.dist-info oauth2client-4.1.3.dist-info
SpeechRecognition-3.8.1.dist-info openpyxl
__pycache__ openpyxl-2.6.2.dist-info
_distutils_hack pdfkit
aiohttp pdfkit-0.6.1.dist-info
aiohttp-3.6.2.dist-info pip
apiclient pip-20.2.4.dist-info
async_timeout pkg_resources
async_timeout-3.0.1.dist-info psycopg2
attr psycopg2-2.8.6.dist-info
attrs-19.3.0.dist-info pyasn1
beautifulsoup4-4.8.2.dist-info pyasn1-0.4.8.dist-info
bs4 pyasn1_modules
cachetools pyasn1_modules-0.2.8.dist-info
cachetools-4.0.0.dist-info pygame
certifi pygame-1.9.6.dist-info
certifi-2019.11.28.dist-info pyperclip
chardet pyperclip-1.8.1.dist-info
chardet-3.0.4.dist-info pytz
coronavirus pytz-2019.3.dist-info
coronavirus-1.1.0.dist-info requests
distutils-precedence.pth requests-2.23.0.dist-info
django requests_file-1.5.1.dist-info
easy_install.py requests_file.py
et_xmlfile rfc3986
et_xmlfile-1.0.1.dist-info rfc3986-1.4.0.dist-info
google rsa
google_api_python_client-1.7.11.dist-info rsa-4.0.dist-info
google_auth-1.11.2-py3.8-nspkg.pth scrape
google_auth-1.11.2.dist-info scrape-0.10.1.dist-info
google_auth_httplib2-0.0.3.dist-info selenium
google_auth_httplib2.py selenium-3.141.0.dist-info
googleapiclient setuptools
h11 setuptools-50.3.2.dist-info
h11-0.9.0.dist-info simpleaudio
h2 simpleaudio-1.0.4.dist-info
h2-3.2.0.dist-info sniffio
hpack sniffio-1.1.0.dist-info
hpack-3.0.0.dist-info soupsieve
hstspreload soupsieve-2.0.dist-info
hstspreload-2020.5.19.dist-info speech_recognition
httplib2 spotipy
httplib2-0.17.0.dist-info spotipy-2.11.2.dist-info
httpx tekore
httpx-0.12.1.dist-info tekore-1.7.0.dist-info
hyperframe tldextract
hyperframe-5.2.0.dist-info tldextract-3.1.0.dist-info
idna twilio
idna-2.9.dist-info twilio-6.35.5.dist-info
image uritemplate
image-1.5.33.dist-info uritemplate-3.0.1.dist-info
imapclient urllib3
jdcal-1.4.1.dist-info urllib3-1.25.8.dist-info
jdcal.py virtualenv
jwt virtualenv-20.0.4.dist-info
lxml yarl
lxml-4.5.0.dist-info yarl-1.4.2.dist-info
I think you are working on two different python interpreters. You can check the version you use in the shell by typing python3 -v on mac. You could also install anaconda and setup a new enviroment to make sure everything is installed

ModuleNotFoundError: No module named 'allennlp.common'

I am getting this error while importing allennlp,
from allennlp.common.util import sanitize
ModuleNotFoundError: No module named 'allennlp.common'
(venv-kbs) administrator#NLR:~/aman/Project$ python
Python 3.6.3 (default, Oct 6 2017, 00:00:00)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import allennlp
>>> from allennlp.common.util import sanitize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'allennlp.common'
My allennlp version is 0.2.1 and even trying to update to 0.4.1 it gives the same error.
TIA
Do check python version, Allennlp keeps pushing updates on regular period and thing works according to Python as well as Pytorch version.

Python3: different behaviour between import and importlib.import_module?

I am unable to dynamically import a module which I have no problem importing in code and I have no idea why.
I have the following:
> ls lib
__init__.py main.py
The init file is empty. The following works:
> python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lib.main
>>> lib.main.sayyay()
yay
The following does not work:
> python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib
>>> importlib.import_module("lib.main")
<module 'lib.main' from '/some/path/lib/main.py'>
>>> lib.main.sayyay()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'lib' is not defined
I did read the importlib documentation as well as a couple of answers here on SO, e.g., How to import a module in Python with importlib.import_module and Dynamically import a method in a file, from a string
But what am I missing?
import_module returns the imported module.
Therefore, you need to give the imported module a name and use this just like lib.main
>>> lib_main = importlib.import_module("lib.main")
>>> lib_main.sayyay()

why can't import pandas after installed successfully?

I have installed pandas with command 'pip3.4 install pandas'.
Successfully installed pandas python-dateutil pytz numpy six
Cleaning up...
root#hwy:~# python3.4
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'pandas'
Why can't import pandas in python3.4 after pandas been installed successfully?
root#hwy:/home/debian8# pip3.4 show pandas
---
Name: pandas
Version: 0.17.1
Location: /usr/local/python3.4/lib/python3.4/site-packages
Requires: python-dateutil, pytz, numpy
root#hwy:/home/debian8# echo "import sys; print sys.path"
import sys; print sys.path
root#hwy:/home/debian8# python3.4
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu',
'/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages',
'/usr/lib/python3/dist-packages']
Your pandas is installed here:
/usr/local/python3.4/lib/python3.4/site-packages
But this path is not in sys.path.
As a workaround do:
export PYTHONPATH=$PYTHONPATH:/usr/local/python3.4/lib/python3.4/site-packages
and inside this terminal start Python again and do your import of pandas.
If this works, add this line above (export PYTHONPATH...) to your ~/.bashrc or equivalent if you use a different shell for a more permanent solution.

Resources