getting sys.path to include the proper root directory on Run

Answered

Hi - I'm a novice at PyCharm, just trying out EAP 1.1, after using the NetBeans Python support for quite a while. How does PyCharm determine what it should add to sys.path when it runs a file? It's not doing what I would expect.

My project directory hierarchy looks like this:

(in /home/me)

MyProject/

  src/

    mymodule/

      __init__.py

      something.py

      ...

      submodule/

        somethingelse.py

I have marked MyProject/src as the "Source Root".

If I run something.py PyCharm prepends /home/me/MyProject/src/mymodule to sys.path (twice).

If I run somethingelse.py it prepends  /home/me/MyProject/src/mymodule/submodule to sys.path (twice).

But I would expect it to add /home/me/MyProject/src to sys.path. (This is what NetBeans does when you designate a source folder.)


something.py has

  from mymodule import Blah

in it, but because of the wrong sys.path, Python can't find mymodule

What's the best way to get the correct sys.path (PYTHONPATH)?

Thanks very much,

Dan

3 comments
Comment actions Permalink

Can someone please comment on why the directory containing the running script is prepended to sys.path? Is this configurable?

0
Comment actions Permalink

Hi tpetricek! I believe this is a standard behavior in Python, when you call python some_script.py current working directory is prepended to sys.path. Do you have a more complex case in mind?

0
Comment actions Permalink

Hi Pavel, I wasn't talking about the current working directory but about the directory which contains the running script. These are two different things. And Pycharm seems to add the containing directory to sys.path, which is not standard behavior. Is this behavior configurable?

1

Please sign in to leave a comment.