Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 337
robert king Avatar asked May 29 '12 11:05

robert king


People also ask

Why import is not working in PyCharm?

Troubleshooting: Try installing/importing a package from the system terminal (outside of PyCharm) using the same interpreter/environment. In case you are using a virtualenv/conda environment as your Project Interpreter in PyCharm, it is enough to activate that environment in the system terminal and then do the test.

How do I fix unresolved reference in Intellij?

The only way to resolve the behavior is to use the "Invalidate Caches / Restart... ->Invalidate and Restart" option.


1 Answers

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

like image 144
Jakob Bowyer Avatar answered Oct 06 '22 01:10

Jakob Bowyer