Rusty Rob
Rusty Rob

Reputation: 17173

trouble importing python modules from other packages (pycharm gives unresolved reference)

I don't know if pycharm is anything to do with the problem, or if it's me being noob.

Here is a screenshot of my folder layout and three code files:

screenshot

I'm using Pycharm on windows and "my_working_directory" is my working directory. You can see in the screenshot that in mod1.py "from package2.mod2" is underlined meaning "unresolved reference".

When I attempt to run mod1.py I get:

C:\Python27\python.exe C:/Users/Robert/PycharmProjects/play/my_working_directory/package1/mod1.py
Traceback (most recent call last):
  File "C:/Users/Robert/PycharmProjects/play/my_working_directory/package1/mod1.py", line 1, in <module>
    from package2.mod2 import SomeClass
ImportError: No module named package2.mod2

When I run main.py I get:

C:\Python27\python.exe C:/Users/Robert/PycharmProjects/play/my_working_directory/main.py
hi

I don't understand why one works and not the other.

The main problem is that pycharm gives me no auto complete and from mod1 it can't detect package2. This is a huge problem because package1 is my view package, package2 is my models or utils or lib package (in a big model view controller). (example https://github.com/coto/gae-boilerplate/blob/master/web/handlers.py pycharm isn't detecting any of the from lib.? import ? statements. (giving unresolved reference).

Anyone know what's going on? Is this the best way of importing packages? Should I expect to be able to have autocomplete?

Should "my_working_directory" be made into a package by making another init.py file?

Upvotes: 6

Views: 8736

Answers (1)

Jakob Bowyer
Jakob Bowyer

Reputation: 34688

It could be that C:/Users/Robert/PycharmProjects/play is not on your %PYTHON_PATH%

Upvotes: 2

Related Questions