I have imported some python module and I would like to view the implementation of some of the module methods. How can I do this?
I tried inspect.getsource, however, I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 701, in getsource
lines, lnum = getsourcelines(object)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 690, in getsourcelines
lines, lnum = findsource(object)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 529, in findsource
raise IOError('source code not available')
IOError: source code not available
Related
I occasionally find myself trying to debug a Python script, for which the Python debugger works nicely most of the time. But sometimes I have trouble finding where an exception has occurred. Consider the following Python script:
a='one'
def myfunc(two,*args,**kwrds):
b='three'
raise Exception('line four')
myfunc('line five')
raise Exception('line six')
Depending on whether line 4 is commented or not, this has a stacktrace
Traceback (most recent call last):
File "/absolute/path/to/myscript.py", line 6, in <module>
raise Exception('line six')
Exception: line six
or
Traceback (most recent call last):
File "/absolute/path/to/myscript.py", line 5, in <module>
myfunc('line five')
File "/absolute/path/to/myscript.py", line 4, in myfunc
raise Exception('line four')
Exception: line four
If I run this under python3 -mpdb myscript.py, the second becomes
Traceback (most recent call last):
File "/usr/local/Cellar/python#3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pdb.py", line 1726, in main
pdb._runscript(mainpyfile)
File "/usr/local/Cellar/python#3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pdb.py", line 1586, in _runscript
self.run(statement)
File "/usr/local/Cellar/python#3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/bdb.py", line 580, in run
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "/absolute/path/to/myscript.py", line 1, in <module>
a='one'
File "/absolute/path/to/myscript.py", line 4, in myfunc
raise Exception('line four')
Exception: line four
Notice that Python is now reporting that myfunc was entered from line 1, the first executable line of the script, instead of line 5. Even worse, if I comment out the Exception within myfunc, the stack trace becomes
Traceback (most recent call last):
File "/usr/local/Cellar/python#3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pdb.py", line 1726, in main
pdb._runscript(mainpyfile)
File "/usr/local/Cellar/python#3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/pdb.py", line 1586, in _runscript
self.run(statement)
File "/usr/local/Cellar/python#3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/bdb.py", line 580, in run
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "/absolute/path/to/myscript.py", line 1, in <module>
a='one'
Exception: line six
Instead of python reporting the Exception as occurring on line 6, it now reports line 1, and there is no way to find where the Exception actually occurred. How can I find the line of code for the stacktrace that corresponds to the top level of my script?
Other things I've tried:
import inspect; print(inspect.stack()[0])
> FrameInfo(frame=<frame at 0x10e0329a0, file '<stdin>', line 1, code <module>>, filename='<stdin>', lineno=1, function='<module>', code_context=None, index=None)
import traceback; traceback.print_stack()
> File "<stdin>", line 1, in <module>
Traceback (most recent call last):
File "c:\msys64\mingw64\share\gdb/python\gdb\__init__.py", line 130, in _execute_file
exec(compiled, globals, globals)
File "C:\Users\AZ\gef\gef.py", line 11583, in <module>
PYTHONBIN = which("python3")
File "C:\Users\AZ\gef\gef.py", line 1738, in which
raise FileNotFoundError(f"Missing file `{program}`")
FileNotFoundError: Missing file `python3`
I cannot find solution to this error on google that's why I decided to ask here.
I've been upgrading our Django/Python app to Python 3.9.7 and Django 3.2.7 (from Python 3.5 and Django 1.11.23).
Currently if I try to run python manage.py createsuperuser
I get the following error
Traceback (most recent call last):
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/selector_events.py", line 261, in _add_reader
key = self._selector.get_key(fd)
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/selectors.py", line 193, in get_key
raise KeyError("{!r} is not registered".format(fileobj)) from None
KeyError: '10 is not registered'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/myuser/Development/app/manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/myuser/.pyenv/versions/insta9/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/Users/myuser/.pyenv/versions/insta9/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/myuser/.pyenv/versions/insta9/lib/python3.9/site-packages/django/core/management/base.py", line 367, in run_from_argv
connections.close_all()
File "/Users/myuser/.pyenv/versions/insta9/lib/python3.9/site-packages/django/db/utils.py", line 213, in close_all
connection.close()
File "/Users/myuser/.pyenv/versions/insta9/lib/python3.9/site-packages/django/utils/asyncio.py", line 19, in inner
event_loop = asyncio.get_event_loop()
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/events.py", line 639, in get_event_loop
self.set_event_loop(self.new_event_loop())
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/events.py", line 659, in new_event_loop
return self._loop_factory()
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/unix_events.py", line 54, in __init__
super().__init__(selector)
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/selector_events.py", line 61, in __init__
self._make_self_pipe()
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/selector_events.py", line 112, in _make_self_pipe
self._add_reader(self._ssock.fileno(), self._read_from_self)
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/selector_events.py", line 263, in _add_reader
self._selector.register(fd, selectors.EVENT_READ,
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/selectors.py", line 523, in register
self._selector.control([kev], 0, 0)
TypeError: changelist must be an iterable of select.kevent objects
Exception ignored in: <function BaseEventLoop.__del__ at 0x103307310>
Traceback (most recent call last):
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/base_events.py", line 683, in __del__
self.close()
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/unix_events.py", line 63, in close
if self._signal_handlers:
AttributeError: '_UnixSelectorEventLoop' object has no attribute '_signal_handlers'
Exception ignored in: <function BaseEventLoop.__del__ at 0x103307310>
Traceback (most recent call last):
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/base_events.py", line 683, in __del__
self.close()
File "/Users/myuser/.pyenv/versions/3.9.7/lib/python3.9/asyncio/unix_events.py", line 63, in close
if self._signal_handlers:
AttributeError: '_UnixSelectorEventLoop' object has no attribute '_signal_handlers'
These are some of the versions I'm using in my requirements.txt
Django==3.2.7
django-braces==1.14.0
django-pipeline==2.0.6
django-storages==1.11.1
djangorestframework==3.12.4
django-localflavor==2.0
django-redis==5.0.0
django_guardian==2.4.0
django-debug-toolbar==3.2.2
django-grappelli==2.15.1
django-extensions==3.1.3
django-simple-history==3.0.0
django-cities-light==3.8.1
django-autoslug==1.9.8
django-constance==2.8.0
django-jsonify==0.3.0
django-bulk-update==2.2.0
django-sslserver==0.22
django-admin-easy==0.6.1
django-admin-rangefilter==0.8.1
django-model-utils==4.1.1
gunicorn==19.7.1
honcho==0.7.1
ipython==5.6.0
eventlet==0.32.0
selenium==2.53.2
This also seems to happen when I runserver, and then kill the server again. Any ideas what could be causing the issue?
Sometimes you don't need async functionality to run the application. In that case set allow async unsafe.
export DJANGO_ALLOW_ASYNC_UNSAFE=True
I am trying to register command-line options in Click. Everything is working fine until I add the params argument to the constructor.
class InitCommand(click.Command):
def __init__(self):
super().__init__(
name='init',
short_help='Initialize the needed scaffolding.',
help='something helpful, but longer',
# params=[] ### <-- Works fine
params=[click.Option('--force', default=False)]
)
I get the following error:
Traceback (most recent call last):
File "/usr/local/bin/aquapy", line 11, in <module>
load_entry_point('aquapy-cli', 'console_scripts', 'aquapy')()
File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 561, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2631, in load_entry_point
return ep.load()
File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2291, in load
return self.resolve()
File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2297, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Users/******/__main__.py", line 3, in <module>
from .bootstrap import main
File "/Users/******/bootstrap.py", line 17, in <module>
main.add_command(InitCommand())
File "/Users/******/commands/init_command.py", line 10, in __init__
params=[click.Option('--force', default=False)]
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1460, in __init__
Parameter.__init__(self, param_decls, type=type, **attrs)
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1266, in __init__
self._parse_decls(param_decls or (), expose_value)
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1533, in _parse_decls
raise TypeError('Name defined twice')
TypeError: Name defined twice
The issue is that click.Option() expects the first argument to a list and is not like the decorator version #click.option().
params=[click.Option(['--force'], default=False)]
Wrapping the command option in a list solved the problem.
This error messages comes up every time
import nltk
nltk.download()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\Lib\site-packages\nltk\downloader.py", line 655, in download
self._interactive_download()
File "C:\Python34\Lib\site-packages\nltk\downloader.py", line 974, in _interactive_download
DownloaderGUI(self).mainloop()
File "C:\Python34\Lib\site-packages\nltk\downloader.py", line 1234, in __init__
self._fill_table()
File "C:\Python34\Lib\site-packages\nltk\downloader.py", line 1530, in _ fill_table
items = self._ds.collections()
File "C:\Python34\Lib\site-packages\nltk\downloader.py", line 499, in collections
self._update_index()
File "C:\Python34\Lib\site-packages\nltk\downloader.py", line 825, in _update_index
ElementTree.parse(compat.urlopen(self._url)).getroot())
File "C:\Python34\lib\xml\etree\ElementTree.py", line 1187, in parse
tree.parse(source, parser)
File "C:\Python34\lib\xml\etree\ElementTree.py", line 598, in parse
self._root = parser._parse_whole(source)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
And the downloader window opens without anything or list. I have checked downloader.py and it contains the right and new default link of the corpora.
I am using Windows standard edition.