question

DodsonArthur-6544 avatar image
0 Votes"
DodsonArthur-6544 asked ·

Unable to run migrations for django web-application with Postgresql database.

Hi,

I have followed this https://docs.microsoft.com/en-us/azure/developer/python/tutorial-python-postgresql-app-portal to deploy my web application onto Azure. I am struggling at the section "Run Django database migrations".

I've successfully deployed my code via Bitbucket, and all the relevant packages have been downloaded, when I click on my website URL this also brings up the login page of my website which is great!

But... I can't seem to run database migrations. I've opened the web ssh, gone into the directory "/home/site/wwwroot#" and activated the virtual environment via "source /antenv/bin/activate". I then try to run "python manage.py migrate" but I receive the error "ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?". So I run pip freeze to check what packages are there, and I'm missing all the packages within my requirements.txt. However, when I navigate to "cd /antenv/lib/python3.8/site-packages", all my packages (such as django) are present. So I'm really confused...! I've also tried to redeploy etc. (Worth noting that I've not had that much experience with deploying web applications.)

Any help or things to check for would be greatly appreciated.

Many thanks.

azure-webappsazure-database-postgresqlazure-webapps-ssh
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @DodsonArthur-6544,

I've been able to reproduce your issue. I'm not sure if the issue is with the repo or the docker image used to run the repo. What I think is happening is that pip install -r requirements.txt isn't running before the wsgi.py is ran but I am investigating.

Regards,
Ryan

0 Votes 0 ·

1 Answer

ryanchill avatar image
0 Votes"
ryanchill answered ·

@DodsonArthur-6544, I was able to get this work when I added my GitHub repo as a deployment source. This kicked off the build on the image and successfully created the virtual environment. What I did before was used Visual Studio Code to deploy the folder to the web app. This is perfectly fine, but you need to make sure you add SCM_DO_BUILD_DURING_DEPLOYMENT as an app setting set to 1. See https://docs.microsoft.com/en-us/azure/app-service/configure-language-python#customize-build-automation for more information.

64781-dgtas5coro.gif

Regards,
Ryan


dgtas5coro.gif (2.4 MiB)
· 4 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Ryan,

Thanks for your message. When I run pip install -r requirements.txt I now run into the error "error: command 'gcc' failed with exit status 1" when it is trying to install channels-Redis and "Running setup.py install for Twisted ... error". But I'm still completely confused because when I navigate to /antenv/lib/python3.8/site-packages/, all the packages in my requirements.txt file seem to be there. But they aren't there when I activate the environment /antenv/bin/activate and do pip freeze. This means that I can't run python manage.py migrate within the environment because I run into a "module not found error". And when I try to run pip install -r requirements.txt anyway, it doesnt work...

Thanks

0 Votes 0 ·

Hi Ryan,

I've managed to solve the problem, I needed to install sudo commands and then install gcc. pip install -r requirements.txt then worked and I was able to migrate my database schema.

(Although I'm still a little confused on why packages seem to be installed at /antenv/lib/python3.8/site-packages/ but when I activate the environment with source/antenv/bin/activate, the packages are not linked. Why is this?)

Anyway, all is good for now.

Thanks a lot for your help.

0 Votes 0 ·
ryanchill avatar image ryanchill DodsonArthur-6544 ·

install gcc shouldn't have been required @DodsonArthur-6544. Can you post your requirements.txt down below? Also, did you add SCM_DO_BUILD_DURING_DEPLOYMENT = 1 as an app setting to your app service?

Regards,
Ryan

0 Votes 0 ·

Sure thing. I've attached my requirements.txt. I did not add SCM_DO_BUILD_DURING_DEPLOYMENT=1 to my app settings since I tried this before and then my app didn't seem to deploy...

65350-requirements.txt


0 Votes 0 ·
requirements.txt (1.7 KiB)