Setting working directory in PyCham doesn't seem to work

Viewed 127

I keep running into path issues in my flask app, when running in PyCharm. I've set the working directory in my run configuration to project-root/api/src, but the relative paths to resources don't resolve properly.

To check what's going on I ran os.getcwd() and I'm getting project-root.

What am I doing wrong? I tested the same project with a colleague with a identical (to my knowledge) configuration and on his machine os.getcwd() properly returns what is expected, project-root/api/src.

I tried reconfiguring the project from scratch, by removing .idea folder and creating the project again in PyCharm, to no avail.

I'm using macOs BigSur, Python 3.9 and PyCharm 2021.2.1. Screenshot below contains my configuration (sensitive info, like company/project name removed):

configuration and debug console output

1 Answers

I managed to solve this issue myself - the reason was that I'm using pipenv, which requires a .env file to be present in the root of the project folder. Flask, when finding such a file, switches the working directory to the root project folder, disregarding any PyCharm setting.

A solution to this is to add a FLASK_SKIP_DOTENV=1 to the application environment variables.

Described in detail here: https://github.com/pallets/flask/issues/3209

Related