VMware Tanzu Application Service for VMs

 View Only

 How to set PYTHONPATH for python application. Please check details section for more understanding.

Anju Thakur's profile image
Anju Thakur posted Jan 29, 2020 04:26 PM

I am having python application with below structure and setting PYTHONPATH environment veriable but not working for me.

Please suggest.​

Python Directory ( root dir) ---> sub directory 1

​ ----> sub directory 2 --> subdirA --->.Entryfile.py ( running webserver will run from Entry file.py).

Command : CF push appname -p pythonDirectory

Command: CF set-env appname PYTHONPATH home/vcap/app

​Note ~ I am pushing exploded format of application.

Daniel Mikusa's profile image
Daniel Mikusa

This one is not totally obvious. The `cf set-env` can work, but the buildpack itself is also going to set PYTHONPATH, which will override your `cf set-env`.

 

What you can do to make this work is to add a `.profile` file to the root of your project, i.e. the directory from which you are running `cf push` or in your case where you point `-p` which is `pythonDirectory`. This file will be uploaded with your app & it will be sourced before your app runs.

 

In that file, just put something like this:

export PYTHONPATH="$PYTHONPATH:$HOME"

That will append a path onto the exiting PYTHONPATH set by the buildpack. It add's $HOME which points to `/home/vcap/app` which is the root of the files that you have pushed. If you wanted to point to a subdirectory, just change it to `$HOME/subdir` or whatever path you need in your project.

 

Hope that helps!

Anju Thakur's profile image
Anju Thakur

Thanks for the information. ​