AttributeError: 'NoneType' object has no attribute '_instantiate_plugins' (Cannot import create_engine)
Asked Answered
H

6

8
import os

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

engine=create_engine(os.getenv("DATABASE_URL"))
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()

Traceback (most recent call last): File "list.py", line 6, in engine=create_engine(os.getenv("DATABASE_URL")) File "C:\Users\Aakash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine__init__.py", line 479, in create_engine return strategy.create(*args, **kwargs) File "C:\Users\Aakash\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\strategies.py", line 56, in create plugins = u._instantiate_plugins(kwargs) AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'

And if change my code to:

The Problem and the traceback is in the picture.

Halfwit answered 15/4, 2020 at 20:27 Comment(0)
P
8

It looks like os.getenv("DATABASE_URL") is returning None. Calling create_engine(None) give you this error. Is DATABASE_URL defined in your environment variable ?

Penetrate answered 15/4, 2020 at 21:29 Comment(0)
B
4

just use this as url "postgresql://username:password@host:port/database" directly pass these values inside your create_engine("postgresql://username:password@host:port/database")

I was having the same problem now its gone.That worked for me. Only thing important to mention is that I got a different error altogether after creating the new user and database and moving the tables. The error was '

'' ModuleNotFoundError: No module named 'psycopg2' '''

and the solution was running: pip3 install psycopg2-binary

PS: URL details with you details.

Balfour answered 6/6, 2020 at 20:38 Comment(0)
C
0

instead of

   engine=create_engine(os.getenv("DATABASE_URL"))
   db = scoped_session(sessionmaker(bind=engine))

type this with your url:

   engine = create_engine("postgresql://scott:tiger@localhost/mydatabase")
   db = scoped_session(sessionmaker(bind=engine))
Canvass answered 8/6, 2020 at 17:21 Comment(2)
Why would you expose your connection string in your codebase?Quaternion
This response does not make any sense whatsoever. If you set the environmental variable correctly, you don't need to expose your credentials.Incursive
B
0

To avoid typing your postgresql connection (including password) in your code define your environment variable in your terminal according this:

source ~/.bash_profile

Or you can use the short form of the command:

. ~/.bash_profile

This executes .bash_profile file in the current shell.

Additional advices concerning reloading .bash_profile can be found in the comments here.

Benoit answered 19/2, 2021 at 10:11 Comment(0)
B
0

In my case, the problem was occurring because I hadn't put the correct path of the file I was trying to upload to my AWS db.

Balthasar answered 8/3, 2022 at 21:28 Comment(0)
F
0

I was having this error because of environment variable in windows.. Environment variable of database(pinot) was not properly configured. check your variable name and key both.

Fishhook answered 11/8, 2022 at 10:2 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Deci

© 2022 - 2024 — McMap. All rights reserved.