AttributeError: 'NoneType' object has no attribute '_instantiate_plugins' while creating engine

Question:

I have a python script(list.py) which is used to interact with postgresql database.

import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(os.getenv("postgresql://postgres:nehal@localhost:5432/lecture3"))
db = scoped_session(sessionmaker(bind=engine))

def main():
    flights = db.execute("SELECT origin, destination, duration FROM flights").fetchall()
    for flight in flights:
        print(f"{flight.origin} to {flight.destination}, {flight.duration} minutes.")

if __name__ == "__main__":
    main()

I have postgresql installed on Ubuntu 16.04 with lecture3 as database.When I execute the code as python list.py,I get the following error:

Traceback (most recent call last):
  File "list.py", line 5, in <module>
    engine = create_engine(os.getenv("postgresql://postgres:nehal@localhost:5432/lecture3"))
  File "/home/nehal/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/__init__.py", line 424, in create_engine
    return strategy.create(*args, **kwargs)
  File "/home/nehal/anaconda3/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 52, in create
    plugins = u._instantiate_plugins(kwargs)
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'

postgres is the postgresql username and nehal is the password.
How do I correct the error?

Asked By: user7441

||

Answers:

I would try to run this without the getenv which seems useless and might return None

create_engine("postgresql://postgres:nehal@localhost:5432/lecture3")
Answered By: Yoav Glazner

os.getenv is used to get the value of an environment variable, and returns None by default if that variable doesn’t exist. You’re passing it your connection string, which (almost certainly) doesn’t exist as an environment variable. So it’s returning None, which is given to create_engine, which fails because it’s expecting a connection string. Just pass your connection string in directly:

engine = create_engine("postgresql://postgres:nehal@localhost:5432/lecture3") 

This work for me:

export DATABASE_URL="postgres://localhost/lecture3"
export DATABASE_URL="postgres://localhost:5432/lecture3"
export DATABASE_URL="postgres:///lecture3"

You should use this command in your ENV.

Answered By: Daniel Elias

Do this before running $ python list.py
where

 username        your psql username
 password        your psql password
 server          localhost or remote server
 port            5432
 database        your psql database
(flask) $ export DATABASE_URL="postgresql://username:password@localhost:5432/database"

Verify:

(flask) $ echo $DATABASE_URL

Run:

$ python list.py
Answered By: rwg

For me the below worked perfectly without any issues

engine = create_engine("postgres://postgres:password@localhost:5432")

where “password” shall be substituted for your PostgreSQL password you used while installing PostgreSQL.

Answered By: syedmeesamali

If you are using windows then setting up in System Variable would resolve the issue. For example on windows 7: click start and type computer, it will bring up computer program, click computer and it will open up computer program, click on “system properties” tab then it will bring up system properties application, now click the “Advanced” tab then you need to click the “Environment Variables” tab, it will open the dialog box which will have user variables and system variables. Click the “New” tab on system variables and then type “DATABASE_URL” in variable name and “postgres+psycopg2://postgres:password@localhost:5432/postgres” in Variable value. Clike “OK”
Open new command prompt using cmd. Change directory to the directory where your list.py program exist. in my case I have it in c:cs50src3 so after opening the cmd then cd c:cs50src3. then execute python list.py you will see your results like

enter image description here

Answered By: shyam
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.