5

I am using flask and sqlite3 on https://www.pythonanywhere.com. In my own machine, when I test the application, I do not need to specify the directory of the database for example db = sqlite3.connect("database.db") and it works perfectly. While on pythonanywhere, I would need to change it to db = sqlite3.connect("/path/to/database.db") because when I do not change I will get this error:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

In /var/log/<application>.pythonanywhere.com.error.log, I get:

OperationalError: no such table: <table-name>

And I found an empty database.db in my home folder at pythonanywhere (which means the application created it, right?) and the database.db I created is in the projects folder

Is there any way to specify the directory in order for it to work perfectly both in my machine and on pythonanywhere without changing the path?

0

1 Answer 1

8

In your db file, try this way:

from os import path

ROOT = path.dirname(path.realpath(__file__))

...
db = sqlite3.connect(path.join(ROOT, "database.db"))
...

Instead of directly pointing out the path, the ROOT always point to the directory of your file that contains the db, then you should get the right location for your database.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It worked perfectly now for both my machine and in pythonanywhere!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.