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()
Related
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)
When working on some legacy Python package, I noticed there was a sub-package shadowing a module with the same name. Here is a simplified file hierarchy showing the problem:
t/
t/__init__.py
t/u/
t/u.py
t/u/__init__.py
As you can see, there is a python module t/u.py and also a sub-package t/u/ It looks like a standard import statement loads the sub-package:
$ python3
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import t.u
__init__.py
>>>
But, is there a way to import the module t/u.py instead?
Maybe from t import u and from t.u import u works
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.
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.
Can anyone help me to figure out what is happening and how to solve this error?
The following message happens when I try to import a module I compiled before:
Python 3.1.4 (default, Mar 8 2012, 09:13:26)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pypt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python3.1/site-packages/pypt.so: undefined symbol: PyUnicodeUCS4_FromString