Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm can't find the right paths if I open a directory that is not the Django root

Our projects are usually structured:

/project-name   /src     /django-project-name       etc.. readme.md requirements.txt 

If I open /project-name instead of /django-project-name PyCharm underlines my imports saying they can't find and it tries to reference imports as src.django-project-name.app_name.models.Thing which can't actually be found when you run Django.

How can I get it to work the same as when I mount /djang-project-name where it gets these things right?

like image 364
Kit Sunde Avatar asked Jun 06 '12 07:06

Kit Sunde


People also ask

How do I give a directory path in PyCharm?

Press Ctrl+Alt+S to open the IDE settings and select Appearance & Behavior | Path Variables. and enter the name of the new variable (for example, DATA_PATH ) and its value that points to the target directory with the data file on your disk. Share the run/debug configuration through your version control system.

How do I change the root directory in PyCharm?

Open the Project Structure settings. In the Projects pane of the Project Structure page, click the project you want to configure content roots for. In the dialog that opens, locate the desired directory and click OK.

How do I see the directory in PyCharm?

In mac's PyCharm this can be done by right clicking on the src/ folder > Mark Directory as > Sources Root. Helped me with lot of similar import issues. Hope this helps someone.

What is source root in PyCharm?

Source roots (or source folders; shown as ). These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports. The files under the source roots are interpreted according to their type.


1 Answers

I fixed it by going to File -> Preferences -> Project Structure selecting the /django-project-name in the tree and clicking sources to add it.

like image 132
Kit Sunde Avatar answered Sep 23 '22 06:09

Kit Sunde