Prevent pycharm & python from prepending `sys.path` with the directory of the ran file

72 views Asked by At

I cannot determine why pycharm & python are prepending sys.path with the directory of the ran file.

Suppose my project layout, called project, with dependency called dependency is structured like this:

projects/
  project/
    app/
      main.py
  dependency/
    lib/
      libfile.py

Pycharm settings:

  • "Working directory" is set to projects/project
  • "add content roots to PYTHONPATH" is checked, and both project and dependency are imported as content roots in the project structure.
  • "add source roots to PYTHONPATH" is not checked and I've not marked any folders as source roots.

main.py:

import sys
list(map(print, sys.path))

Running file.py with pycharm with these settings results in a path that starts with projects/project/app even though I'm setting the "working directory" to projects/project

pycharm main.py output:

projects/project/app
projects/project
projects/dependency
(...site packages...)

and if run from a terminal in /projects, PYTHONPATH=/projects/project:/projects/dependency python app/main.py yields main.py output:

projects/project/app
projects/project
projects/dependency
(...site packages...)

so still not what I want but it matches pycharm.

QUESTION: How do I prevent python/pycharm from adding projects/project/app to the start of sys.path?

0

There are 0 answers