Home > Software design >  Why does my Flask try to import from a different directory?
Why does my Flask try to import from a different directory?

Time:12-21

I'm trying to run the following code and on line 3 I'm getting an error which states

'Import Error: cannot import name 'declaritive_base' from 'sqlalchemy.ext.declartive' (C:\users\timot\flasky\venv\lib\site-packages\sqlalchemy\ext\declarative_init_.py)'

I can see that when I'm running the file with py model.py it shows that it is digging around in the above directory. This is NOT where I want it looking for these packages. I want it looking for the below instead.

C:\users\timot\flasky2\flasky\venv\lib\site-packages\sqlalchemy\

Which for some reason pip install SQLalchemy and pip install flask_SQLalchemy didn't install to the flasky2 subdirectories listed above?

The parent directoy for model.py is in flasky2/flasky/venv

Can anyone tell me how I can tell my environment to install and open libraries from the flasky2/flasky/venv lib folder?

model.py for those who are interested.

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declartive_base
from sqlalchemy.orm import sessionmaker, relationship

Base = declartive_base()

class User(Base):
    __tablename__ = "person"

    id = Column('id', Integer, primary_key=True, nullable=False)
    username = Column('username', String, unique=True, nullable=False)
    emailAddress = Column('emailAddress', String, unique=True, nullable=False)
    password = Column('password', String, nullable=False)
    streetNumber = Column('streetNumber', String, nullable=False)
    suburb = Column('suburb', String, nullable=False)
    state = Column('state', String, nullable=False)
    postCode = Column('postCode', String, nullable=False)
    cardNumber = Column('cardNumber', String, nullable=False)
    cardCVC = Column('cardCVC', String, nullable=False)
    cardExpiry = Column('cardExpiry', String, nullable=False)

engine = create_engine('sqlite:///:db.py', echo=True)
base.metadata.create_all(bind=engine)

CodePudding user response:

Short answer: It is because your python interpreter is probably pointing to a different location. Redirect to the right one in your virtual environment and it will work.

Points to help you out

How to locate the path to your current python interpreter?

If you are using git bash:

$ which python

or

you can also create a script:

import sys
print(sys.executable)

Now that you know the path to your right python interpreter. If you are using VScode (which a recommend) click on the interpreter

enter image description here

Now, insert the path to your desired python interpreter here. enter image description here

It will do the job. If you still having troubles with that lemme know

  • Related