Excluded folders are included in Python path

Answered

Are excluded folders supposed to be excluded from the Python interpreter path, or just from searches etc.?  I have directories with Python libraries installed for other platforms, which I use for deployment and want available from within Pycharm, but I don't want them to feature in the Python path.  I was fairly sure in the last version I had them in the project, marked them as excluded and they were excluded, but that doesn't seem to be the case in 2018.2.  

Have I misunderstood the purpose of this?  I've tried invalidating caches and restarting etc.

0
5 comments

An update on this - I found the console preference which allows me to choose whether to include roots in the python path (content roots and source roots are separate).  I think this will solve my problem (although I'm still interested to know whether 'excluding' directories is intended to also prevent them from acting as roots - I thought it used to do this in 2018.1.  

I now have a different question - I added a directory as a content root and have not marked it as a source root, yet it still gets added to the path when I restart the console.  Is there anything that overrides this setting?  Other directories I've added seem to be obeying these console config options.

0

Further update - there was one particular folder (archive) that was still being added to python path despite not being a source root.  I found that by explicitly setting the Python console's working directory to the project root, that this archive folder was then NOT added, so I have a working setup.  But the implication is that for some reason this arbitrary folder was being used as as the default working directory rather than the project root.  I have an instance of Pycharm running in Windows as well as MacOS and the behaviour is exactly the same on both.  Is this expected behaviour?

0

I'm still struggling with this and could use some help!

In preferences -> Console -> Python Console, I have "add content roots" unchecked and "add source roots" checked.

Locally, I test Lambda functions on the Mac with the virtual environment.  I have a directory added to the project as a content root, that is not in my project root called "linux_python", that contains Linux versions of the libraries I use for my Lambda functions.  I have marked this directory as "Excluded" in the project tree, so that the Linux libraries are NOT used in preference to the local ones.

I create a .py file in the project root - "test.py", which is just a hello world but with an import that exists in both the local library and the excluded linux directory.

Observations:

1. When the first run configuration is created for this test file, it doesn't pick up the include settings from preferences -> Console -> Python Console.  Both content roots and source roots are checked as included.

2. When I run this configuration, I get an error that relates to a missing path to a module in a source root (which is checked to be included).  If I check "include content roots" as well, I then get an import error which tells me it's finding the library (psycopg2) in the excluded directory linux_python.

3. If I check "run with Python Console" and run it again, it works, and I can see that the correct source roots are being included in sys.path.extend.

This seems like a bug to me:

A. The path extensions behave differently depending on whether a module is run with the python console or not.  When running without the Python Console, source roots are NOT added to the path even when checked, but content roots are.

B. Excluded folders are NOT excluded from path includes (in either case).

Please can someone take a look?

 

0

PyCharm simply ignores Excluded folders, it doesn't remove them from PYTHONPATH.

>When running without the Python Console, source roots are NOT added to the path even when checked, but content roots are

Could you please create a ticket in our bug tracker https://youtrack.jetbrains.com/issues/PY and provide your project tree and Python Console settings screenshots? It's hard to replicate the exact setup according to your instructions.

 

0

Thanks for the clarification, although I'd argue it's not a question of removing them from the path, it's a question of not adding them when the "add content roots" box is checked.  But if this is by design, it would be fine if it weren't for the other issue.

Obviously, any folder I add to the project is a content root.  So excluding it is moot provided the settings to "add content roots to PYTHONPATH" is cleared, and actually works.  At the moment, it seems that checking the "add source roots to PYTHONPATH" does NOT work unless the "Run using Python Console" box is checked.  "Add content roots" does work, but this then adds the folders I don't want included.

I created the ticket earlier anyway.  I can go and add some screenshots, but it would be more straightforward to create a project from scratch and create several versions of a python module to see what's happening when you run a test.  I can probably do that and attach the project file.

--EDIT - I get exactly the same behaviour in Windows so it's not a local machine thing or a MacOS thing

0

Please sign in to leave a comment.