0

I have a python code called list.py which is used so I can interact with local postgresql database named data_sample. This is my code:

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

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

When I execute the code as python list.py, I get the following error:

Traceback (most recent call last):
  File "list.py", line 6, in <module>
    engine = create_engine(os.getenv("data_sample"))
  File "/Users/admin/.pyenv/versions/3.7.3/lib/python3.7/site-packages  /sqlalchemy/engine/__init__.py", line 500, in create_engine
    return strategy.create(*args, **kwargs)
  File "/Users/admin/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 56, in create
    plugins = u._instantiate_plugins(kwargs)
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'

It seems like it doesn't know my database called data_sample. How do I fix this?

7
  • are you certain that the os.getenv() is correct? afaik it gets the value for the environment variable specified. Commented Jul 7, 2020 at 8:17
  • I see you've updated the code; but you didn't update the traceback, if it is the same error. Commented Jul 7, 2020 at 8:19
  • the traceback is still the same, i just edited it due to some typos I've made Commented Jul 7, 2020 at 8:28
  • but the traceback still shows your 'postgresl' code, which isn't in your code. Commented Jul 7, 2020 at 8:30
  • oh yeah, I didn't realize that. thanks Commented Jul 7, 2020 at 8:33

1 Answer 1

0

Refer this link

os.getenv is used to get the value of environment variable

Use this instead:

engine = create_engine("postgresql://username:password@localhost:port/name_of_database")

If you haven't changed the default settings then your username will be postgres and port will be 5432.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.