6

I'm trying to run the list.py code on my Ubuntu machine, but I'm getting the following error:

AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'

I looked at this post and it when I run psql $DATABASE_URL (from the comments), I get

Warning: No existing local cluster is suitable as a default target. Please see man pg_wrapper(1) how to specify one.
Error: You must install at least one postgresql-client-<version> package

but I did run the requirements.txt from Project 1.

[Update: ran sudo apt-get install postgresql-client, but the issue with list.py persists]

Brian says he has the database variable set to localhost. How can I run the lecture3 codes on my local machine?

  • i am running the same on windows 10 it shows the same error how can i rectify this? – KHAN AYAAN Sep 1 '19 at 12:22
  • Half baked lecture. Easy things are shown, hard things solve yourself. Wah – TheOnlyAnil Apr 26 at 16:36
9

Postgresql creates a default user called postgres, so login as default user and create a new user

sudo -i -u postgres
createuser --interactive

Then enter your linux username and select Yes for superuser option.

Exit the postgre user by typing exit.

With your user, create a new database by typing

createdb mydb

In the database, you can create your table flights and add (or insert) values into the table.

To run list.py, you can change line #7 to engine = create_engine("postgresql:///mydb") and run python list.py. If all is correct, you should see the output:

New York to London, 415 minutes.
Shanghai to Paris, 760 minutes.
Istanbul to Tokyo, 700 minutes.
New York to Paris, 435 minutes.
Moscow to Paris, 245 minutes.
Lima to New York, 455 minutes.
| improve this answer | |
  • i am running the same on windows 10 i had tried whatever you suggested but it still shows the same problem. – KHAN AYAAN Sep 1 '19 at 12:23
  • You should follow this form when writing the database url "postgresql://username:password@host:port/database". This should work if you created a new user, created a new database, created a table and inserted some columns. – Mahdy Mousa Hamad Mar 17 at 21:37
  • 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. After all this, I could run the lecture3 exercises locally and see the output correctly. Many thanks! – Juan Carlos Marcos Prieto May 1 at 19:47
3

For Windows users:

1- Create a new user on SQL Shell:

CREATE ROLE your_user_name LOGIN PASSWORD 'your_password' NOINHERIT CREATEDB;

2- Create a new database on SQL Shell:

Close the SQL shell and open it again, enter the new username and password for the user you just created.

To create a new database, write the following command:

CREATE DATABASE mydb;

You should connect to the new database we just created, write the following command:

\c mydb

3- Create a new table on SQL Shell

CREATE TABLE flights(
    id SERIAL PRIMARY KEY,
    origin VARCHAR NOT NULL,
    destination VARCHAR NOT NULL,
    duration INTEGER NOT NULL);

4- Make sure to insert some rows into your table

5- Write the database URL correctly

The URL should follow a specific pattern:

postgresql://your_username:your_password@localhost:5432/mydb

We have two options now:

1- You can use this URL directly in your code

engine = create_engine("postgresql://.....@localhost:5432/mydb")

2- Or create a new environment variable with the name DATABASE_URL and a value of the URL.


I hope some of you find this helpful :)

| improve this answer | |
  • Thank you very much Mahdy Mousa Hamad!. It's worked on Window. I have read hundred of documentation but this is the best and most simple answer. – Trung Hiếu Đỗ Apr 12 at 9:56
  • You'er welcome Trung, glad you find my answer helpful : ) – Mahdy Mousa Hamad Apr 13 at 15:43
  • This worked for me! – Richard Chimama Apr 17 at 15:14
  • This doen't work for me. – Shahbaaz Apr 21 at 20:21
  • This not working for me. – Nitin Garg Apr 22 at 4:25
1

the problem seems to be with the os module, I can run the code without any problem if I specify url='postgresql://localhost' in the code. I seem to struggle to get values of environment variables I set up.

I use Anaconda - running the database and setting the envir var in Anaconda terminal + running the list.py code in Spyder.

I tried multiple ways of setting the variable, including manually adding them in Windows...

When I run print(os.getenv("DATABASE_URL")), I get None every time.

| improve this answer | |
  • You can check if you wrote if __name__ == '__main__': with the quotes on __name__ because it was the case for me... – Keung Eliot May 9 at 8:46
1

1- Create a new user on SQL Shell:

CREATE ROLE your_user_name LOGIN PASSWORD 'your_password' NOINHERIT CREATEDB; 2- Create a new database on SQL Shell:

Close the SQL shell and open it again, enter the new username and password for the user you just created.

To create a new database, write the following command:

CREATE DATABASE mydb; You should connect to the new database we just created, write the following command:

\c mydb 3- Create a new table on SQL Shell

CREATE TABLE flights( id SERIAL PRIMARY KEY, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL); 4- Make sure to insert some rows into your table

5- Write the database URL correctly

The URL should follow a specific pattern:

postgresql://your_username:your_password@localhost:5432/mydb We have two options now:

1- You can use this URL directly in your code

engine = create_engine("postgresql://.....@localhost:5432/mydb") 2- Or create a new environment variable with the name DATABASE_URL and a value of the URL.

| improve this answer | |
  • how to create the new environment variable?! – Ahmed Asker Jun 22 at 12:44
0

I tried aboove method on windows but nothing worked for me. I am getting the follwing error:

Traceback (most recent call last):
  File "D:\python\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "D:\python\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "D:\airline0\env\Scripts\flask.exe\__main__.py", line 9, in 
  File "d:\airline0\env\lib\site-packages\flask\cli.py", line 967, in main
    cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
  File "d:\airline0\env\lib\site-packages\flask\cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "d:\airline0\env\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "d:\airline0\env\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "d:\airline0\env\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "d:\airline0\env\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "d:\airline0\env\lib\site-packages\click\decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "d:\airline0\env\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "d:\airline0\env\lib\site-packages\flask\cli.py", line 848, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "d:\airline0\env\lib\site-packages\flask\cli.py", line 305, in __init__
    self._load_unlocked()
  File "d:\airline0\env\lib\site-packages\flask\cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "d:\airline0\env\lib\site-packages\flask\cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "d:\airline0\env\lib\site-packages\flask\cli.py", line 240, in locate_app
    __import__(module_name)
  File "D:\airline0\application.py", line 9, in 
    engine = create_engine(os.getenv("postgresql://postgres:123456@localhost:5433/mydb"))
  File "d:\airline0\env\lib\site-packages\sqlalchemy\engine\__init__.py", line 479, in create_engine
    return strategy.create(*args, **kwargs)
  File "d:\airline0\env\lib\site-packages\sqlalchemy\engine\strategies.py", line 56, in create
    plugins = u._instantiate_plugins(kwargs)
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'

Can anyone help me to fix this

| improve this answer | |
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review – MARS Jul 5 at 17:39
0

I have tried the above method...
it did not work in cmd...
but it worked in anaconda prompt

also it asked me to install psycopg2 module
YOU CAN INSTALL IT BY THE FOLLOWING PROMPT

pip install psycopg2

HOPE IT WORKS OUT...

| improve this answer | |
0

1- Create a new user on SQL Shell:

CREATE ROLE your_user_name LOGIN PASSWORD 'your_password' NOINHERIT CREATEDB; 2- Create a new database on SQL Shell:

Close the SQL shell and open it again, enter the new username and password for the user you just created.

To create a new database, write the following command:

CREATE DATABASE mydb; You should connect to the new database we just created, write the following command:

\c mydb 3- Create a new table on SQL Shell

CREATE TABLE flights( id SERIAL PRIMARY KEY, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL); 4- Make sure to insert some rows into your table

5- Write the database URL correctly

The URL should follow a specific pattern:

postgresql://your_username:your_password@localhost:5432/mydb Now:

1- You can use this URL directly in your code engine = create_engine("postgresql://your_username:your_password@localhost:5432/mydb")

6- On your PostgreSQL shell type the following command:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO your_user_name;

7- Optionally you can try and install:-

pip install psycopg2

This worked for me.......

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .