user6035109
user6035109

Reputation: 343

Pycharm can't recognize changes in sys.path

Some background - I am working on a project, and have been writing tests with PyTest. The structure of the folders is as following:

src/
    ...
tests/
    helpers/
        fixtures.py
        functions.py
    conftest.py
    test1.py
    test2.py
    ...

The helpers sub-folder holds functions and fixtures which should be accessible across all the tests. In order to do so I added the following to conftest.py:

import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), 'helpers'))

from fixtures import fixture1, fixture2, ....

This actually works, fixtures 1,2,.. are all available across the different tests.

And also:

import functions

works correctly from any file in the tests folder.

But the problem is that PyCharm can't recognize this change, and refer to the modules fixtures, functions as valid, which cancels the auto completion and gives pretty annoying false inspection comments.

If someone knows how to fix it (Notify PyCharm on the update), or maybe give another alternative for how things should be done (that's why I gave all of this context), I will be very grateful.

Upvotes: 1

Views: 1317

Answers (1)

nizarcan
nizarcan

Reputation: 535

In the project view on the left, you should right-click on the helpers folder and click on:

Mark Directory as->Sources Root, and this should resolve your problem.

After doing that, there will be no need for sys.path.append(os.path.join(os.path.dirname(__file__), 'helpers')) within PyCharm.

But since you already have a working system and trying to get rid of the annoying false error, only marking directory as source root should do the trick just fine.

Upvotes: 4

Related Questions