Error while connecting to postgresql using sqlalchemy












0















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?










share|improve this question



























    0















    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?










    share|improve this question

























      0












      0








      0








      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?










      share|improve this 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?







      python postgresql sqlalchemy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 17 '18 at 12:55









      user7441user7441

      8819




      8819
























          5 Answers
          5






          active

          oldest

          votes


















          2














          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") 





          share|improve this answer



















          • 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











          • 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



















          0














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



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





          share|improve this answer































            0














            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.






            share|improve this answer































              0














              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





              share|improve this answer

































                0














                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.






                share|improve this answer























                  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
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  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









                  2














                  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") 





                  share|improve this answer



















                  • 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











                  • 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
















                  2














                  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") 





                  share|improve this answer



















                  • 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











                  • 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














                  2












                  2








                  2







                  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") 





                  share|improve this answer













                  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") 






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 17 '18 at 19:27









                  Nathan VērzemnieksNathan Vērzemnieks

                  2,7651616




                  2,7651616








                  • 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











                  • 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





                    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








                  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













                  0














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



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





                  share|improve this answer




























                    0














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



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





                    share|improve this answer


























                      0












                      0








                      0







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



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





                      share|improve this answer













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



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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 17 '18 at 19:17









                      Yoav GlaznerYoav Glazner

                      6,07211028




                      6,07211028























                          0














                          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.






                          share|improve this answer




























                            0














                            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.






                            share|improve this answer


























                              0












                              0








                              0







                              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.






                              share|improve this answer













                              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.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jul 17 '18 at 2:43









                              Daniel EliasDaniel Elias

                              12




                              12























                                  0














                                  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





                                  share|improve this answer






























                                    0














                                    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





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      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





                                      share|improve this answer















                                      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






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 23 '18 at 20:11









                                      Llazar

                                      9701616




                                      9701616










                                      answered Nov 23 '18 at 17:56









                                      rwgrwg

                                      11




                                      11























                                          0














                                          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.






                                          share|improve this answer




























                                            0














                                            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.






                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              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.






                                              share|improve this answer













                                              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.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jan 17 at 6:55









                                              syedmeesamalisyedmeesamali

                                              112




                                              112






























                                                  draft saved

                                                  draft discarded




















































                                                  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.




                                                  draft saved


                                                  draft discarded














                                                  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





















































                                                  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







                                                  Popular posts from this blog

                                                  Wiesbaden

                                                  Marschland

                                                  Dieringhausen