14

I started evaluating PyCharm 3 professional edition because I will be working on several Pyramid + SQLAlchemy projects. One of the things I would really love to have is SQLAlchemy autocomplete.

I created a new starter project with the alchemy scaffold following these instructions. I also installed the SQLAlchemy package for the interpreter and virtual environment I am using for this project.

Also, when I created a new pycharm project for this code, the IDE suggested me to install the pyramid, sqlalchemy and other packages. Of course I accepted the suggestion and let the IDE install all of those packages.

In the models.py file, the DBSession is declared as follows:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

In the views.py file, the DBSession is used this way:

one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()

So I started playing with the IDE and did something like this: typed DBSession. and the IDE just gave me some few suggestions, within which the 'query' function was not listed. Then I tried typing: DBSession.query(MyModel). and pressed Ctrl+Space to try to get suggestions and a 'No suggestions' message showed up.

I would really like to have the SQLAlchemy suggestions of functions that I could use on my DBSession variable (like filter, filter_by, first, etc). I would say that this is mandatory for me :)

Is there something I am missing? Or, PyCharm doesn't support this?

Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29
Roland Pish
  • 815
  • 1
  • 9
  • 21
  • Are you using a virtual environment? – kylieCatt Aug 13 '14 at 04:53
  • Yes, I am using a virtual environment, but I realized that this is probably not related to PyCharm since it may not deduce that a certain variable is of a certain type in the editor. So I had to learn to live without that autocomplete. – Roland Pish Aug 13 '14 at 17:21
  • Go to `File > Settings > Python Interpreter` and make sure your virtual env where SQLAlchemy is installed is selected. Sometimes it will pick the wrong interpreter and you lose auto completion support. – kylieCatt Aug 13 '14 at 17:23
  • @IanAuld, I checked that and the virtualenv is selected as the python interpreter of the project. Does it have to do that the virtualenv is in a virtual machine on the same computer? – Roland Pish Aug 13 '14 at 19:24

4 Answers4

16

The solution I've found to this (picked up from somewhere on the web) was to type hint the DBSession instance like this:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
""":type: sqlalchemy.orm.Session"""

After this the code completion seems to work fine everywhere in the project

1

Note that the tutorial states:

This guide was written for PyCharm 2.7.3, although many of the topics apply for PyCharm 3.

In PyCharm 3 Professional, it is much easier to install Pyramid and start using a scaffold. See one of my video tutorials Pyramid in PyCharm in 5 minutes at 1:17 specifically.

Also you might want to blow away your project and start fresh if stuff doesn't work as expected.

PyCharm 3 Professional supports SQAlchemy as follows.

  • Code insight (2.6+)
  • Possibility to view database structure in a diagram. Refer to the section Working with Diagrams.
  • Code completion and resolve. (3.0+)

See more information on how to use code completion.

Steve Piercy
  • 13,693
  • 1
  • 44
  • 57
  • Thanks Steve. Your video was very helpful! Unfortunately I started fresh and created a new Pyramid project using 'alchemy' scaffold and I still wasn't able to get any code completion for the DBSession sqlalchemy object (also took a look again at the 'code completion' information). Still fighting with this and trying to figure out how to get the sqlalchemy autocomplete before the 30 day trial ends. Any other suggestion is greatly appreciated. – Roland Pish Feb 15 '14 at 06:38
  • Thanks! It seems for me, that PyCharm Community Edition just doesn't support autocompletion unlike Professional one – Artem Zaytsev Jun 17 '15 at 21:16
1

I use a type declaration after a variable assignment:

from sqlalchemy import create_engine
from sqlalchemy.engine import Engine

...

engine = create_engine(connect_str, max_overflow=10)
engine: Engine

As a use for variables in for loop, I used:

for table, meta in tables.items():
    meta: Table
    pass

in which, tables is sqlalchemy.orm.mapper.Mapper, and table is an imported type:

from sqlalchemy import create_engine, Table
Fify
  • 131
  • 1
  • 8
1

If anyone gets here now, the best solution I'v seen for this issue can be found here. To save you the click:

from contextlib import contextmanager
from typing import ContextManager

@contextmanager
def session() -> ContextManager[Session]:
    yield Session(...)
A. Kali
  • 739
  • 6
  • 19