0

Python: 3.8.6

OS: Windows

IDE: PyCharm

I have 2 files: X.py and Y.py that are in different directories from each other.

X is mapped in source control while Y is not. Y contains the statement import X. This works fine when X and Y are in the same directory. If they are not, then on hovering above the import X statement, PyCharm says 'No module named X'.

Following this SO answer, I added sys.path.insert(1, 'path/to/folder/containing/X') above import X but still get the error.

How can I ensure that X is visible to Y.py?

Note: The reason I am doing this and not following the typical package structure is because I do not want to move X out of source control or Y into source control.

v81
  • 57
  • 1
  • 6
  • Do you get a `ModuleNotFoundError` when you _run_ `python X.py` or do you only get the error in PyCharm when hovering over the `import` ? – TheEagle Jul 12 '21 at 21:34
  • what if you leave out the `X` at the end of the path? – Clemens Klein-Robbenhaar Jul 12 '21 at 21:58
  • @ClemensKlein-Robbenhaar I have not included X. The path specified in the question was phrased poorly - it should have been sys.path.insert(1, ') – v81 Jul 13 '21 at 20:56
  • @Programmer looks like you are correct - I don't get an error on actually running it. Guess I trusted PyCharm a little too much :) – v81 Jul 13 '21 at 20:57
  • @v81 PyCharm must have an option to add additional folders where it should look for modules - if it has, adding the directory containing `X.py` there should fix the error. – TheEagle Jul 13 '21 at 21:38

0 Answers0