I'm trying to run pydoc on a Python file using my Windows machine. The file contains SQLAlchemy models, but have been struggling with an error.
I appreciate it if I can receive some guidance. Attached below: the command that I execute, the target Python file and the error that I'm getting.
My command is as follows:
python -m pydoc -w objects
Here is my Python file, objects.py.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
app.app_context().push()
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:mycoolpassword@localhost:5432/mydb'
db = SQLAlchemy(app)
class Book(db.Model):
"""
Docstring for my class
Col1
Col2
"""
__tablename__ = 'mytable'
col1 = db.Column(db.Integer, primary_key = True)
col2 = db.Column(db.Integer)
db.drop_all()
db.create_all()
Here is the error that I'm receiving.
$ python -m pydoc -w objects
Traceback (most recent call last):
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 2744, in <module>
cli()
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 2704, in cli
writedoc(arg)
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 1697, in writedoc
page = html.page(describe(object), html.document(object, name))
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 380, in document
if inspect.ismodule(object): return self.docmodule(*args)
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 738, in docmodule
contents.append(self.document(value, key, name, fdict, cdict))
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 381, in document
if inspect.isclass(object): return self.docclass(*args)
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 843, in docclass
for name, kind, cls, value in classify_class_attrs(object)
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\pydoc.py", line 204, in classify_class_attrs
for (name, kind, cls, value) in inspect.classify_class_attrs(object):
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\inspect.py", line 443, in classify_class_attrs
srch_obj = getattr(srch_cls, name, None)
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_sqlalchemy\model.py", line 30, in __get__
return cls.query_class(
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\orm\query.py", line 273, in __init__
self._set_entities(entities)
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\orm\query.py", line 282, in _set_entities
self._raw_columns = [
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\orm\query.py", line 283, in <listcomp>
coercions.expect(
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\coercions.py", line 413, in expect
resolved = impl._literal_coercion(
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\coercions.py", line 651, in _literal_coercion
self._raise_for_expected(element, argname)
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\coercions.py", line 1139, in _raise_for_expected
return super()._raise_for_expected(
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\coercions.py", line 710, in _raise_for_expected
super()._raise_for_expected(
File "C:\Users\Boco\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\coercions.py", line 535, in _raise_for_expected
raise exc.ArgumentError(msg, code=code) from err
sqlalchemy.exc.ArgumentError: Column expression, FROM clause, or other columns clause element expected, got <class 'sqlalchemy.orm.decl_api.Model'>.
I tried to reinstall flask and SQLAlchemy and ensure that all dependencies such as psycopg2-binary are all installed.
1 Answer 1
It seems that pydoc has a bug when it comes to Python files with DB models. I ended up using pdoc and it generated the documentation with no issues.
pip install pdoc
pdoc objects.py
help()then 'shell returned 1'. Does your module impor tother modules by any chance, which could have queries in their top-level code?