There is an error that shouldn't be there or doesn't make sence.
I expected the code to work, since it's for a lecture and the code is the exact code. The code should be working, in the lecture it is, but mine gives an error.
The python code:
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("{flight.origin} to {flight.destination}, {flight.duration} minutes.")
if __name__ == "__main__":
main()
The error:
Traceback (most recent call last):
File "list.py", line 6, in
engine = create_engine(os.getenv("DATABASE_URL"))
File "/Users/gebruiker/Library/Python/2.7/lib/python/site-packages/sqlalchemy/engine/__init__.py", line 488, in create_engine
return strategy.create(args, *kwargs)
File "/Users/gebruiker/Library/Python/2.7/lib/python/site-packages/sqlalchemy/engine/strategies.py", line 56, in create
plugins = u._instantiate_plugins(kwargs)
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'
Versions.
code is fine. that looks like your DATABASE_URL environmnet variable is not set:
>>> from sqlalchemy import create_engine
>>> create_engine(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 2, in create_engine
File "/home/classic/dev/sqlalchemy/lib/sqlalchemy/util/deprecations.py", line 210, in warned
return fn(*args, **kwargs)
File "/home/classic/dev/sqlalchemy/lib/sqlalchemy/engine/create.py", line 441, in create_engine
plugins = u._instantiate_plugins(kwargs)
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'
code is fine. that looks like your DATABASE_URL environmnet variable is not set:
>>> from sqlalchemy import create_engine >>> create_engine(None) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 2, in create_engine File "/home/classic/dev/sqlalchemy/lib/sqlalchemy/util/deprecations.py", line 210, in warned return fn(*args, **kwargs) File "/home/classic/dev/sqlalchemy/lib/sqlalchemy/engine/create.py", line 441, in create_engine plugins = u._instantiate_plugins(kwargs) AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'
What would then be examples of variables that could be set in the place of 'DATABASE_URL' ? I'm quite new at this and don't really know what is meant by that.
In Python, the "os.getenv()" call is used to access what are known as environment variables, which are variables that are set outside of the program itself. In Windows, you would set these variables using a command like SET. In Linux or OSX, the "export" command is used.
A good introduction to the concept of environment variables is in Wikipedia at https://en.wikipedia.org/wiki/Environment_variable.
To see how an environment variable works, you can run the "export" command and then run your Python shell, here is an illustration:
[classic@photon3 ~]$ export DATABASE_URL="sqlite://"
[classic@photon3 ~]$ export ANOTHER_DATABASE_URL="postgresql://scott:tiger@localhost/test"
[classic@photon3 ~]$ python
Python 3.7.6 (default, Jan 30 2020, 09:44:41)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getenv("DATABASE_URL")
'sqlite://'
>>> os.getenv("ANOTHER_DATABASE_URL")
'postgresql://scott:tiger@localhost/test'
>>> os.getenv("A_NAME_I_DIDNT_SET")
>>> os.getenv("A_NAME_I_DIDNT_SET") is None
True
so if I were to use this to make a SQLAlchemy engine, it would look like:
[classic@photon3 ~]$ export DATABASE_URL="sqlite://"
[classic@photon3 ~]$ python
Python 3.7.6 (default, Jan 30 2020, 09:44:41)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sqlalchemy import create_engine
>>> import os
>>> engine = create_engine(os.getenv("DATABASE_URL"))
>>> connection = engine.connect()
>>> connection.execute("select 'we are connected'").fetchall()
[('we are connected',)]
I hope this clarifies.
Most helpful comment
In Python, the "os.getenv()" call is used to access what are known as environment variables, which are variables that are set outside of the program itself. In Windows, you would set these variables using a command like SET. In Linux or OSX, the "export" command is used.
A good introduction to the concept of environment variables is in Wikipedia at https://en.wikipedia.org/wiki/Environment_variable.
To see how an environment variable works, you can run the "export" command and then run your Python shell, here is an illustration:
so if I were to use this to make a SQLAlchemy engine, it would look like:
I hope this clarifies.