Python, matplotlib loads using ipython but not using python - python

When I try to load matplotlib using python I get an error about loading the backports.functools_lru_cache module. When I try to load matplotlib using ipython it loads just fine.
As near as I can tell ipython uses the same version of python that calling python uses. I've gone through the path that both python and ipython use and uninstalled matplotlib and backports.functools_lru_cache. I've tried then reinstalling using apt and pip, always uninstalling the previous attempt before trying the next.
I've gone through a bunch of stackexchange solutions and tried some github solutions without any luck. I run Ubuntu 18.04.
Error:
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 127, in <module>
from matplotlib.rcsetup import defaultParams, validate_backend, cycler
File "/usr/lib/python2.7/dist-packages/matplotlib/rcsetup.py", line 29, in <module>
from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
File "/usr/lib/python2.7/dist-packages/matplotlib/fontconfig_pattern.py", line 32, in <module>
from backports.functools_lru_cache import lru_cache
ImportError: No module named functools_lru_cache
I won't show ipython, because it loads just fine.
Update:
If I install using:
pip install matplotlib==2.0.2
matplotlib works for both python and ipython.

The problem appears to be a path problem I am not sure how to solve. I use apt on Ubuntu 18.04 to install system modules into /usr/lib/python2.7/dist-packages/. This directory is where backports.functools_lru_cache lives. I also have some compiled modules installed in /usr/local/lib/python2.7/dist-packages/ where there is a backports module WITHOUT functools_lru_cache. The problem is that python is not following the system path and only seems to be looking in /usr/local/lib/python2.7/dist-packages/ for backports. However, ipython looks in both which is why ipython was working while python was not.
python:
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/brendan/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0']
>>> import backports
>>> backports.__path__
['/usr/local/lib/python2.7/dist-packages/backports']
ipython:
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: sys.path
Out[1]:
['',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/home/brendan/.local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/gtk-2.0']
In [2]: import backports
In [3]: backports.__path__
Out[3]:
['/usr/local/lib/python2.7/dist-packages/backports',
'/usr/lib/python2.7/dist-packages/backports']
Solution:
To solve the problem I manually copied functools_lru_cache.py from /usr/lib/python2.7/dist-packages/backports/ to /usr/local/lib/python2.7/dist-packages/backports/. Now I can run matplotlib 2.2.2 no problem in python and ipython.
I'm going to mark this answered because my question now revolves around python and path.

Related

Python and iPython submodule import differences

I recently migrated my solution using pytz to dateutil.tz. I've tested everything with iPython, and it worked nicely. However, when in production with python... ERROR!
Can anyone explain why Python and iPython deal differently with the imports, and if there is a way to make python behave the same way as iPython does?
$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type "help", "copyright", "credits" or "license" for more information.
>>> import dateutil
>>> dateutil.__file__
'/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
>>> dateutil.tz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'dateutil' has no attribute 'tz'
>>>
$ ipython
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import dateutil
In [2]: dateutil.__file__
Out[2]: '/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
In [3]: dateutil.tz
Out[3]: <module 'dateutil.tz' from '/usr/local/lib/python3.8/site-packages/dateutil/tz/__init__.py'>
Because IPython wants to be too clever. For any reason it has already imported dateutil.tz and when you manually import dateutil, then dateutil.tz is available in you main namespace.
But as is is a submodule, you should consistently import it if you want to use it. So you should replace import dateutil with import dateutil.tz and your code will work fine on both environment.
$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
Type "help", "copyright", "credits" or "license" for more information.
>>> import dateutil.tz
>>> dateutil.__file__
'/usr/local/lib/python3.8/site-packages/dateutil/__init__.py'
>>> dateutil.tz
<module 'dateutil.tz' from '/usr/local/lib/python3.8/site-packages/dateutil/tz/__init__.py'>

How to import a package that use absolute import in python

I'm trying to import volatility3 into my python project/script, so that I don't have to use os.system since volatility3 is already made in python3.
I'm wondering how can I import all the functions/modules of said project ? The functions I'm interested in are located in volatility3/volatility/framework
I tried simply putting:
>>> import volatility3.volatility.framework
But I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/volatility3/volatility/framework/__init__.py", line 12, in <module>
from volatility.framework import constants, interfaces
ModuleNotFoundError: No module named 'volatility'
My guess is I have to modify sys.path or one of the path variables but this does not seem to work.
Thanks,
The best solution here would be to properly install volatility with pip3, from your already exiting repository folder:
$ pip3 install /home/volatility3
or directly from pipy (not tested):
$ pip3 install volatility3
then you should be able to import the from volatility package directly:
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import volatility.framework
>>> volatility.framework
<module 'volatility.framework' from '/home/bruno/.local/lib/python3.6/site-packages/volatility/framework/__init__.py'>
>>>

psutil can not be found within a flask project

I have setup a fairly simple flask project. Within this flask project I want to use psutil but its not being found with the import statement.
> $ ./satelite.py
Traceback (most recent call last):
File "./satelite.py", line 2, in <module>
from app import app
File "/home/neil/monitor/satelite/app/__init__.py", line 4, in <module>
from app import views
File "/home/neil/monitor/satelite/app/views.py", line 6, in <module>
import psutil
ImportError: No module named psutil
However when I use python cli it is.
> $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> print psutil.cpu_percent()
15.3
The same can be said for just a straight forward python script being executed separately.
Standard python modules(subprocess, os, etc) are loading fine. I have tried to focus my google-fu on this but I am getting nowhere so would massively appreciate if anyone can point me in the right direction.
Cheers
Does your first line in satelite.py refers to the same python binary as which python in your terminal ?
(This refer to the #! line)
Maybe you are using python3 in your satelite.py file.

IDLE in virtualenv does not function correctly?

I installed numpy and other packages in virutalenv vpy1. I can correct import those packages if I invoke python command in console after activated the vpy1.
However, if I use IDLE, it cannot import those packages:
>>> import numpy
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import numpy
ImportError: No module named numpy
I then found out the IDLE does not have correct virtualenv setting. If I invoke python in console, the setting will be like this:
(vpy1)xxx ~ $ python
Python 2.7.6 (default, Jun 1 2014, 03:20:25)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.real_prefix
'/home/xxx/python-2.7.6'
>>> sys.executable
'/home/xxx/virtualenvs/vpy1/bin/python'
if I use IDLE:
(vpy1)xxx ~ $ which idle
/home/xxx/virtualenvs/vpy1/bin/idle
(vpy1)xxx ~ $ idle
#in IDLE
Python 2.7.6 (default, Jun 1 2014, 03:20:25)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> sys.executable
'/home/xxx/python-2.7.6/bin/python2.7'
>>> hasattr(sys,'real_prefix')
False
How can I fix this?
Finally solved this problem. In the vpy1/bin the idle is directly link to /home/xxx/python-2.7.6/bin/idle, which using a wrong shebang. Change the shebang to #!/usr/bin/env python solve this problem.

PyQt4 can't import Qsci

I'm installing PyQt4 on an old Linux system (CentOS 4.4) that can't be upgraded for hardware compatibility reasons. I've got Python 2.6, QT4 and SIP installed, and the installation of PyQt4 didn't give me any errors.
When I run Python, this happens:
Python 2.6.2 (r262:71600, May 11 2011, 14:18:37)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt4
>>> import PyQt4.Qsci
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Qsci
I looked around, and found /usr/local/lib/python2.6/site-packages/PyQt4/uic/widget-plugins/qscintilla.py, which is almost empty of non-comment stuff:
pluginType = MODULE
def moduleInformation():
return "PyQt4.Qsci", ("QsciScintilla", )
Any ideas?
You need to install qscintilla separately. If PyQt is already installed, then you should just have to install the qscintilla python bindings. Hopefully this fixes your issue!

Categories

Resources