Flask bad interpreter

1.1k Views Asked by At

Following this tutorial: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database I've reached the part where I'm supposed to create my first db and migrate it's tables.

Having db_create.py, in the root directory of my xyzTest app, with the following contents:

#!flask/bin/python
from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
from app import db
import os.path
db.create_all()
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
    api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
    api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
    api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO,
                        api.version(SQLALCHEMY_MIGRATE_REPO))

Whether I execute script with the virtualenv active (virtualenv flask; source flask/bin/activate), or not, the results of the execution of the script is identical.

./db_create.py returns:

-bash: ./db_create.py: flask/bin/python^M: bad interpreter: No such file or directory

python db_create.py returns:

Traceback (most recent call last):
File "db_create.py", line 2, in from migrate.versioning import api ImportError: No module named 'migrate'

Yet, both flask-sqlalchemy and sqlalchemy-migrate are installed, along with the flask framework its self.

ppip list

argparse (1.2.1)
Babel (1.3)
blinker (1.3)
coverage (3.7.1)
decorator (3.4.2)
Flask (0.10.1)
Flask-Babel (0.9)
Flask-Login (0.2.11)
Flask-Mail (0.9.1)
Flask-OpenID (1.2.4)
Flask-SQLAlchemy (2.0)
Flask-WhooshAlchemy (0.56)
Flask-WTF (0.11)
flipflop (1.0)
guess-language (0.2)
itsdangerous (0.24)
Jinja2 (2.7.3)
MarkupSafe (0.23)
pbr (0.11.0)
pip (1.5.6)
python-openid (2.2.5)
pytz (2015.4)
setuptools (5.5.1)
six (1.9.0)
speaklater (1.3)
SQLAlchemy (0.9.9)
sqlalchemy-migrate (0.9.6)
sqlparse (0.1.15)
Tempita (0.5.2)
Werkzeug (0.10.4)
Whoosh (2.7.0)
wsgiref (0.1.2)
WTForms (2.0.2)
2

There are 2 best solutions below

0
On BEST ANSWER

Searching to find the meaning of ^M, I encountered this article Unix script appends ^M at end of each line

I have applied same solution as the selected answer, dos2unix db_create.py and fixed it.

It happened because I'm writing from a windows machine, into a folder linux folder. I executed the following command: find ./ -name "*.py" -not -path "./flask/*" -exec dos2unix {} \; to ensure that I won't have same issue with any other file.

0
On

It could be a problem with the sqlalchemy-migrate version. The tutorial is utilizing sqlalchemy-migrate v.07.2 (see the comments section of the tutorial link you posted in the question). Also the version of sqlalchemy in the tutorial is older (from the author "You should be using sqlalchemy 0.7.9 with sqlalchemy-migrate. That project was not updated to work with sqlalchemy 0.8.")