0

Total python noob here, I want to run my .py flask api file, i cd to my directory, activate my python virtual env, then i try run.py

Issue is that it prints:

$ python run.py                                             

Traceback (most recent call last): File "run.py", line 1, in from flask_sqlalchemy import SQLAlchemy ImportError: No module named flask_sqlalchemy

As far as im aware as im in my virtualenv and have my packages installed, this shouldnt happen. Trying to pip3 install on any packages shows them as already existing.

To further assist with the issue, i tried to run the script inside pycharm and i get the following error prints in the log:`Traceback (most recent call last):

  File "/Users/Jack/Developer/Python/lift_api/run.py", line 26, in <module>
    sqlAlchemy.create_all() 
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 1007, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 999, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 941, in get_engine
    return connector.get_engine()
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 543, in get_engine
    self._engine = rv = sqlalchemy.create_engine(info, **options)
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/engine/__init__.py", line 387, in create_engine
    return strategy.create(*args, **kwargs)
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 80, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/Users/Jack/Developer/Python/lift_api/liftapi/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 554, in dbapi
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'`

Anyone have an idea?

jwarris91
  • 902
  • 9
  • 24

2 Answers2

3

To run your code with python 3.x use python3 command.

python3 run.py
Shreyash S Sarnayak
  • 2,309
  • 19
  • 23
0

What is saying what it needs - psycopg2. If you are on Ubuntu it can help:

sudo apt install libpq-dev python-dev

Then don't forget to install flask-sqlalchemy and other libraries while your virtualenv activated.

Nurjan
  • 5,889
  • 5
  • 34
  • 54
  • $ pip3 install psycopg2 Requirement already satisfied: psycopg2 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages – jwarris91 Jun 25 '17 at 13:24
  • @jwarris91, Sorry, are you using windows? – Nurjan Jun 25 '17 at 13:26
  • I am using a mac, it shouldnt make a difference if we are just installing packages, which doesnt seem to actually be the issue – jwarris91 Jun 25 '17 at 13:27
  • @jwarris91 Have you tried to install psycopg2 globally? Without virtualenv? – Nurjan Jun 25 '17 at 13:30
  • why would i want to do that? that would be many steps backward and not resolve the issue – jwarris91 Jun 25 '17 at 13:30
  • @jwarris91 Try to install psycopg2. I had something similar in my practice and it helped me. – Nurjan Jun 25 '17 at 13:31
  • @jwarris91 Try install this package globally the way it is done on MacOS - `python-psycopg2`. – Nurjan Jun 25 '17 at 13:35
  • no, im not installing anything globally, thats incredible poor practice, it needs to be in the virtual env not external – jwarris91 Jun 25 '17 at 13:36
  • @jwarris91 I know very well what virtualenv is for. I do agree with you that you don't want to install it globally, but there are things which you have to install globally - without them some of the things don't work even in your virtualenv. – Nurjan Jun 25 '17 at 13:38