-1

I have a Flask app currently running on Google App Engine. Locally, the app is running inside a virtual environment and references the appropriate libraries installed in the venv/Lib/site-packages directory.

When the app is updated in GAE, requirements.txt is used to determine what libraries/dependencies need to be installed. I frequently get tedious errors like "Module not found" and have to remember to add said module in my requirements.txt, and then have to redeploy and check the error logs, which takes time.

I have a bunch of dependencies installed in my virtual environment, only some of which need to be referenced in my requirements.txt file, since I only use a few in my Flask app. So, I am trying to figure out a way to test my app locally as if it was running on GAE by forcing Flask to reference only those dependencies in my requirements.txt file so if there is a "Module not found" error, I won't have to repeat gcloud app deploy and have to scour through the logs all over again but rather just do it quickly on my own machine.

Hopefully that wasn't to convoluted, lol.

davidism
  • 121,510
  • 29
  • 395
  • 339
Jack Scott
  • 358
  • 1
  • 4
  • 13

2 Answers2

2
  1. To be clear, not everything installed in your virtual env needs to be declared in your requirements.txt file. Some libraries are installed because they are dependencies of another. For example, just listing Flask will lead to Jinja also being installed
  2. To your specific issue, you're basically saying you did not narrow down the actual libraries you need for your project. This is usually due to copying over installed libraries from another project.
  3. You can use pip3 freeze > requirements.txt or pip2 freeze > requirements.txt to auto-generate your requirements.txt file. The problem with this method is that it will include everything installed in your virtual env and it seems you don't want this.
  4. Some suggest using pipreqs (see this Stackoverflow answer).
  5. I normally do it the manual way i.e. Remove the existing venv, create a requirements.txt with just the basics - python, flask/django, run your program and then manually add each library it complains about into the requirements.txt file and reinstall the contents of your requirements.txt file. Rinse & Repeat till you no longer get errors. Now you have your full requirements.
NoCommandLine
  • 5,044
  • 2
  • 4
  • 15
0

1.Install venv --- sudo apt install python3-venv --- for python3 2.You create Virtual environment for all flask server. Choose some directory and run this command. python3 -m venv venv 3.Run this command for activate for venv source venv/bin/activate 4.Choose diroctory flask server pip3 freeze > requirements.txt -run this command 5. Finally run this command pip3 install -r requirements.txt 6. You can use this venv all flask server. You can update it.