from XYZ import * — pyCharm not seeing it (python 2.7)

Answered

This code executes fine from within the client's test framework:

from XYZ import *

However, when trying to setup a pyCharm project to navigate the code, it only sees module XYZ if init.py files are stored at every level from the sources folder*** to the module folder.

***< sources folder >/path/to/module/XYZ.py

The sources folder is: (a) included in the PYTHONPATH environment variable (b) set as a sources root in pyCharm (c) added to the paths in the project's interpreter settings

Somehow the test framework is able to find it since it executes the test scripts fine, but pyCharm can't find it unless I create package init.py files and then update the import statement to:

from path.to.module.XYZ import *

I'm using PyCharm 2018.2.4.  What is going on?

Thanks

 

5 comments
Comment actions Permalink

Hi Sgrow55,

Thank you for contacting PyCharm support.

Have you tried `File | Invalidate Caches / Restart...`?

Please attach a screenshot showing the problem.

 

Kind regards,

Sergey

 

0
Comment actions Permalink

Yes.  Invalidate cache and restart, as well as restarting the pc after updating environment variables.  I cannot provide a screenshot due to proprietary reasons, but the popup message when hovering over a function call from the import is "Unresolved reference", and when attempting Ctrl-B is "Cannot find declaration to go to".  The same for hovering over the import itself.

0
Comment actions Permalink

Have you marked the directory with XYZ.py as a Source root in Project Structure? (Settings/Preferences | Project | Project Structure)

0
Comment actions Permalink

No ( I did not mark the folder for XYZ.py - < sources folder >/path/to/module - as source root).  But my understanding is if a parent folder - or any folder in the parent path - is marked as sources root, then all subfolders are marked as well.  Actually quite frankly, I don't understand why that would not be the case.  Unless this is a ribbing?  I'm supposed to respond with 'are you effing serious?'.

Totally confused ~

0
Comment actions Permalink

When marking some parent folder as Sources, PyCharm will not look for your module in all subfolders, it will look for the module in this specific directory. If you need to import some module from subdirectory, then you can either mark it as Sources or use already known to you `from path.to.module.XYZ import *`.

There could be other cases and usages of such features so the configuration depends on your needs.

Kind regards,

Sergey

0

Please sign in to leave a comment.