Error while connecting to postgresql using sqlalchemy
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?
python postgresql sqlalchemy
add a comment |
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?
python postgresql sqlalchemy
add a comment |
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?
python postgresql sqlalchemy
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?
python postgresql sqlalchemy
python postgresql sqlalchemy
asked Feb 17 '18 at 12:55
user7441user7441
8819
8819
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
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")
1
Thank you!..But in a statement like thisengine = create_engine(os.getenv("DATABASE_URL"))
where do I set the environment variable DATABASE_URL?
– user7441
Feb 17 '18 at 23:50
That depends on your operating system. Based on the paths in your traceback it looks like you probably are running Linux. There's some good info here: unix.stackexchange.com/q/117467
– Nathan Vērzemnieks
Feb 18 '18 at 3:26
add a comment |
I would try to run this without the getenv
which seems useless and might return None
create_engine("postgresql://postgres:nehal@localhost:5432/lecture3")
add a comment |
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.
add a comment |
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
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48841505%2ferror-while-connecting-to-postgresql-using-sqlalchemy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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")
1
Thank you!..But in a statement like thisengine = create_engine(os.getenv("DATABASE_URL"))
where do I set the environment variable DATABASE_URL?
– user7441
Feb 17 '18 at 23:50
That depends on your operating system. Based on the paths in your traceback it looks like you probably are running Linux. There's some good info here: unix.stackexchange.com/q/117467
– Nathan Vērzemnieks
Feb 18 '18 at 3:26
add a comment |
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")
1
Thank you!..But in a statement like thisengine = create_engine(os.getenv("DATABASE_URL"))
where do I set the environment variable DATABASE_URL?
– user7441
Feb 17 '18 at 23:50
That depends on your operating system. Based on the paths in your traceback it looks like you probably are running Linux. There's some good info here: unix.stackexchange.com/q/117467
– Nathan Vērzemnieks
Feb 18 '18 at 3:26
add a comment |
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")
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")
answered Feb 17 '18 at 19:27
Nathan VērzemnieksNathan Vērzemnieks
2,7651616
2,7651616
1
Thank you!..But in a statement like thisengine = create_engine(os.getenv("DATABASE_URL"))
where do I set the environment variable DATABASE_URL?
– user7441
Feb 17 '18 at 23:50
That depends on your operating system. Based on the paths in your traceback it looks like you probably are running Linux. There's some good info here: unix.stackexchange.com/q/117467
– Nathan Vērzemnieks
Feb 18 '18 at 3:26
add a comment |
1
Thank you!..But in a statement like thisengine = create_engine(os.getenv("DATABASE_URL"))
where do I set the environment variable DATABASE_URL?
– user7441
Feb 17 '18 at 23:50
That depends on your operating system. Based on the paths in your traceback it looks like you probably are running Linux. There's some good info here: unix.stackexchange.com/q/117467
– Nathan Vērzemnieks
Feb 18 '18 at 3:26
1
1
Thank you!..But in a statement like this
engine = create_engine(os.getenv("DATABASE_URL"))
where do I set the environment variable DATABASE_URL?– user7441
Feb 17 '18 at 23:50
Thank you!..But in a statement like this
engine = create_engine(os.getenv("DATABASE_URL"))
where do I set the environment variable DATABASE_URL?– user7441
Feb 17 '18 at 23:50
That depends on your operating system. Based on the paths in your traceback it looks like you probably are running Linux. There's some good info here: unix.stackexchange.com/q/117467
– Nathan Vērzemnieks
Feb 18 '18 at 3:26
That depends on your operating system. Based on the paths in your traceback it looks like you probably are running Linux. There's some good info here: unix.stackexchange.com/q/117467
– Nathan Vērzemnieks
Feb 18 '18 at 3:26
add a comment |
I would try to run this without the getenv
which seems useless and might return None
create_engine("postgresql://postgres:nehal@localhost:5432/lecture3")
add a comment |
I would try to run this without the getenv
which seems useless and might return None
create_engine("postgresql://postgres:nehal@localhost:5432/lecture3")
add a comment |
I would try to run this without the getenv
which seems useless and might return None
create_engine("postgresql://postgres:nehal@localhost:5432/lecture3")
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 Feb 17 '18 at 19:17
Yoav GlaznerYoav Glazner
6,07211028
6,07211028
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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 Jul 17 '18 at 2:43
Daniel EliasDaniel Elias
12
12
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 23 '18 at 20:11
Llazar
9701616
9701616
answered Nov 23 '18 at 17:56
rwgrwg
11
11
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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 Jan 17 at 6:55
syedmeesamalisyedmeesamali
112
112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48841505%2ferror-while-connecting-to-postgresql-using-sqlalchemy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown