Declare App Dependencies
Page last updated:
Page last updated:
Cloud Foundry recognizes an app as Python by the existence of a requirements.txt
file in the root directory. For your own apps, you can create one by running pip freeze > requirements.txt
.
The demo app you deployed already has a requirements.txt
, and it looks something like this:
Flask>=0.11
The requirements.txt
file determines the dependencies that should be installed with your application. When an app is deployed, Cloud Foundry reads this file and installs the appropriate dependencies using the pip install -r requirements.txt
command.
To do this locally, create a virtualenv for the app:
$ virtualenv venv
Then, activate the virtualenv.
If you are using Windows, run this command:
> venv\Scripts\activate.bat
If you are not using Windows, run this command:
$ source venv/bin/activate
Now run this command in your local directory to install the dependencies, preparing your system for running the app locally:
$ pip install -r requirements.txt Collecting Flask>=0.11 (from -r requirements.txt (line 1)) Downloading Flask-0.11.tar.gz (544kB) ...
Once dependencies are installed, you will be ready to run your app locally.
Upon running cf push
, we will also push our whole virtual environment folder venv
. Since Cloud Foundry runs pip install -r requirements.txt
anyways, this is redundant and should be omitted to save bandwidth and even more importantly: time. You can create a .cfignore
file which works just like a .gitignore
file and which tells Cloud Foundry what files should be excluded when pushing. Let’s create a .cfignore
file and exclude the venv
folder:
venv
Note: Cloud Foundry uses the latest version of Python 2 by default. To change that, create a runtime.txt
file and put the respective version number into it (e.g. python-3.6.1
). Be aware however, that Cloud Foundry only accepts the version specified here.