Trace files/modules loaded when launching python - python

I am using Paraview 4.0.1 under Ubuntu 14.04 LTS.
But I guess the answer to this question does not necessarily requires knowledge specific to Paraview python.
When I load a python prompt at the command line everything "works well":
$ pvpython
Python 2.7.6 (default, Oct 26 2016, 20:33:43)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from paraview.simple import *
paraview version 4.0.1
>>>
But when I load the python shell within the GUI, from paraview.simple import * fails, either when automatically (at startup) or explicitly imported:
Python 2.7.6 (default, Oct 26 2016, 20:33:43)
[GCC 4.8.4] on linux2
from paraview.simple import *
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/paraview/simple.py", line 41, in <module>
import servermanager
File "/usr/lib/python2.7/dist-packages/paraview/servermanager.py", line 3157, in <module>
__initialize()
File "/usr/lib/python2.7/dist-packages/paraview/servermanager.py", line 3148, in __initialize
c = Connection(iter.GetCurrentSessionId(), iter.GetCurrentSession())
File "/usr/lib/python2.7/dist-packages/paraview/servermanager.py", line 1935, in __init__
_createModules(self.Modules)
File "/usr/lib/python2.7/dist-packages/paraview/servermanager.py", line 2567, in _createModules
m.filters = createModule('filters')
File "/usr/lib/python2.7/dist-packages/paraview/servermanager.py", line 2629, in createModule
if (prop.GetInformationOnly() and propName != "TimestepValues" ) \
AttributeError: 'NoneType' object has no attribute 'GetInformationOnly'
>>> from paraview.simple import *
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/paraview/servermanager.py", line 2190, in find_module
if vtkPVPythonModule.HasModule(fullname):
AttributeError: 'NoneType' object has no attribute 'HasModule'
>>>
How can I trace the different sequence of steps taken by each mode of loading a python shell, so as to identify the source of error?
In addition to this manifestation, I found two other differences:
In the command prompt, from paraview.simple import * is not executed automatically, while in the GUI it is. Where is this set?
In the command prompt, having set PYTHONSTARTUP=${HOME}/.pythonrc, which sets import readline, rlcompleter (actually, within a try-except construct), allows for tab completion.
In the GUI, .pythonrc is not read. Why is this? How can I tell the filename which would be read upon startup if it exists?

I have found one option which gives some information, I do not know if it is the only/best one.
Setting
$ export PYTHONVERBOSE=2
(or a different integer for verbosity level [1]) gives a lot of info.
Still, I couldn't find the source of error...

Related

EOF Error when importing python package

I am working on a project which utilizes the wbdata package for python, which is a wrapper for the world bank api. It was working fine until I made what I have guessed to be to large of a request of data, which I had to cancel, and now the package won't even load when I use the import wbdata function. Here's what I get
Python 2.7.7 |Anaconda 2.0.1 (64-bit)| (default, Jun 11 2014, 10:40:02) [MSC
v.1500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 2.1.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
? -> 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.
%guiref -> A brief reference about the graphical user interface.
In [1]: import wbdata as wb
Traceback (most recent call last):
File "<ipython-input-1-ae1872a5f07c>", line 1, in <module>
import wbdata as wb
File "C:\Users\Josh New\Anaconda\lib\site-packages\wbdata\__init__.py", line 23, in <module>
from .api import (get_country, get_data, get_dataframe, get_panel,
File "C:\Users\Josh New\Anaconda\lib\site-packages\wbdata\api.py", line 31, in <module>
from . import fetcher
File "C:\Users\Josh New\Anaconda\lib\site-packages\wbdata\fetcher.py", line 104, in <module>
if not len(CACHE.cache)== 0:
File "C:\Users\Josh New\Anaconda\lib\site-packages\wbdata\fetcher.py", line 82, in cache
cache = pickle.load(cachefile)
EOFError
In [2]:
Could someone please help me out with getting this package working again?
Thanks
Josh
EDIT- Ok after running the %debug cachefile.name I get the following;
%debug cachefile.name
NOTE: Enter 'c' at the ipdb> prompt to continue execution.
> c:\users\josh new\anaconda\lib\encodings\cp1252.py(15)decode()
14 def decode(self,input,errors='strict'):
---> 15 return codecs.charmap_decode(input,errors,decoding_table)
16
ipdb> c
Traceback (most recent call last):
File "C:\Users\Josh New\Anaconda\lib\site-packages\IPython\core\magics\execution.py", line 817, in _run_with_debugger
deb.run(code, code_ns)
File "C:\Users\Josh New\Anaconda\lib\bdb.py", line 400, in run
exec cmd in globals, locals
File "<string>", line 1, in <module>
NameError: name 'cachefile' is not defined
Use ipython's magic %debug and check what cachefile's file name is. If it's a regular file, name should be accessible as cachefile.name.
Then nuke that file.
I think I have fixed it by going the wbdata folder in Appdata\Local\wbdata and deleting the cache file that was in it. seems to work now.

Python3 on Ubuntu giving errors on help() command

I used help() in the python3 shell on Ubuntu 14.04
I got this output
Please help , don't know whats wrong.
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/_sitebuiltins.py", line 98, in __call__
import pydoc
File "/usr/lib/python3.4/pydoc.py", line 65, in <module>
import platform
File "/home/omega/platform.py", line 2, in <module>
print("System : ",platform.uname().system)
AttributeError: 'module' object has no attribute 'uname'
>>>
The problem is that platform is the name of a stdlib module, which help uses. By creating a module of your own with the same name that occurs before the stdlib in your sys.path, you're preventing Python from using the standard one.
The fact that your own platform module tries to use the stdlib module of the same name just compounds the problem. That isn't going to work; your import platform inside that module is just importing itself.
The solution is to not collide names like this. Look at the list of the standard modules, and don't create anything with the same name as any of them if you want to use features from that module, directly or indirectly.
In other words: Rename your platform.py to something else, or put it inside a package.
File "/home/omega/platform.py", line 2, in <module>
print("System : ",platform.uname().system)
This is the problem, go to platform.py and fix it, it will be ok. It says, platform has not any method called uname you probably misstyped.

How to use .so modules from Python 2 in Python 3?

probably my question is obvious but I was not able to find an obvious decision.
There are Python 2.6+ extensions called audit and auparse. These are dynamical libraries distributed with audit-libs-python package:
[vitaly#thermaltake tmp]$ repoquery -lq audit-libs-python
/usr/lib64/python2.7/site-packages/_audit.so
/usr/lib64/python2.7/site-packages/audit.py
/usr/lib64/python2.7/site-packages/audit.pyc
/usr/lib64/python2.7/site-packages/audit.pyo
/usr/lib64/python2.7/site-packages/auparse.so
I would like to use this extension in the up-to-date Python interpreter because of suspicions about the incorrect handling of dynamic memory in python 2.6+. For some reason I cannot load them from Python 3.3:
[vitaly#thermaltake ~]$ python3.3
Python 3.3.2 (default, Mar 5 2014, 08:21:05)
[GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("/usr/lib64/python2.7/site-packages/")
>>> import auparse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /usr/lib64/python2.7/site-packages/auparse.so: undefined symbol: _Py_ZeroStruct
>>> import audit
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/audit.py", line 28, in <module>
_audit = swig_import_helper()
File "/usr/lib64/python2.7/site-packages/audit.py", line 24, in swig_import_helper
_mod = imp.load_module('_audit', fp, pathname, description)
File "/usr/lib64/python3.3/imp.py", line 183, in load_module
return load_dynamic(name, filename, file)
ImportError: /usr/lib64/python2.7/site-packages/_audit.so: undefined symbol: PyInstance_Type
I would be glad if anyone could clarify the procedure of importing the modules of this kind into the modern Python interpreter. It's hard to believe that backward compatibility between 2nd and 3rd branches was broken in this case too. Thank you.
.so modules have to be compiled for each specific Python version - you can't even reuse an .so module built for Python 2.6 with Python 2.7.
When crossing over to Python 3 it gets worse, since there are some API changes, and the SO simply won't build unchanged from the .C file (with possible exceptions).
One workaround is to serve the functions you want to use in the 2.6 module with xmlrpc, then call then from a separate Python process runing Python 3.x - that should be the simplest way.

inconsistent behaviour of interactive session and script in Python

When I run the script, I had this ImportError:
$ python ~/Dropbox/code/py/ZoteroFindOrphanedFiles.py
Traceback (most recent call last):
File "/home/zane/Dropbox/code/py/ZoteroFindOrphanedFiles.py", line 1, in <module>
import sqlite3
File "/usr/lib/python3.2/sqlite3/__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "/usr/lib/python3.2/sqlite3/dbapi2.py", line 23, in <module>
import datetime
File "/usr/lib/python3.2/datetime.py", line 20, in <module>
import math as _math
File "/home/zane/Dropbox/code/py/math.py", line 3, in <module>
from nzmath.rational import Integer, Rational
ImportError: No module named nzmath.rational
But I don't have it when running the interactive session:
$ python
Python 3.2.3 (default, Apr 23 2012, 23:14:44)
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>>
Why is that?
Here is the problem:
import math as _math
File "/home/zane/Dropbox/code/py/math.py", line 3, in <module>
You have your own module called math.py, but this is the same as a standard Python module of the same name. This is not recommended.
The solution is to rename your math.py to something else, and don't forget to delete the math.pyc in the same directory (otherwise you'll still have the same problem).
You have a local file
/home/zane/Dropbox/code/py/math.py
which is getting imported instead of the standard lib math module.
The solution is to rename your /home/zane/Dropbox/code/py/math.py to something else.
The problem occurs when you call a script in the /home/zane/Dropbox/code/py directory since this adds this directory to the beginning of sys.path and thus this directory gets searched first when Python tries to import modules.
You have a file called math.py in the script's directory, and this shadows the stdlib math module. Rename the file.

Python 2.5 interpreter is throwing a PyNoAppError when I run >>>> help('modules')

My Python interpreter is giving me an ugly error when I try to pull up a list of all the accessible modules (see below). I've googled "wx._core.PyNoAppError: The wx.App object must be created first!" and I haven't found a good explanation of how to fix it in this context. Can anyone help me out?
Python 2.5.4 (r254:67916, Aug 1 2011, 15:52:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>> help('modules')
Please wait a moment while I gather a list of all available modules...
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pkgutil.py:110: DeprecationWarning: twisted.flow is unmaintained.
__import__(name)
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/twisted/words/im/__init__.py:8: UserWarning: twisted.im will be undergoing a rewrite at some point in the future.
warnings.warn("twisted.im will be undergoing a rewrite at some point in the future.")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site.py", line 348, in __call__
return pydoc.help(*args, **kwds)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pydoc.py", line 1645, in __call__
self.help(request)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pydoc.py", line 1682, in help
elif request == 'modules': self.listmodules()
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pydoc.py", line 1803, in listmodules
ModuleScanner().run(callback)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pydoc.py", line 1854, in run
for importer, modname, ispkg in pkgutil.walk_packages():
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pkgutil.py", line 110, in walk_packages
__import__(name)
File "/var/tmp/wxWidgets/wxWidgets-13~231/2.5/DSTROOT/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/wxaddons/__init__.py", line 29, in <module>
File "/var/tmp/wxWidgets/wxWidgets-13~231/2.5/DSTROOT/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/wx-2.8-mac-unicode/wx/_misc.py", line 3298, in __init__
wx._core.PyNoAppError: The wx.App object must be created first!
if you call help('modules') then, as defined in your site.py, pydoc.help() is called.
This results in a call to Helper.listmodules() which then calls the ModuleScanner() which is 'An interruptible scanner that searches module synopses' as per its docstring.
Unfortunately not all modules/packages are capable of being scanned, as that includes importing the module. You are however lucky that you just get an error. On my system (Linux, Python 2.7.3) this gives a segmentation fault

Categories