0

For some reasons, I can't import my own modules in this format import module.

IDE: Pycharm, Python version: 3

files:

projectname/scr/test1.py
projectname/scr/test2.py

test1.py:

def print_value(x)
    print x

test2.py:

import test1

 value = 5
 test1.print_value(value)

Pycharm Editor shows me the following error.

import test1 # Error: No module named test1

However, when I run the code (python test2.py), it works fine. The only problem is that I do not get recommendations for test1 functions or variables such as when I type test., nothing pops up as a suggestion. It's a bit weird behavior from pycharm.

However, if I move these files up the order in the folder hierarchy, then everything works fine, no errors and proper suggestions are given when I type test1.something.

files:

projectname/test1.py
projectname/test2.py

Any explanation to this? Why doesn't it work in src folder?

Update: Resolved the issue by marking the src as root sources inspired by this StackOverflow question. I still do not understand why it happened and why setting this directory as root source resolve this issue but it works for now.

utengr
  • 3,225
  • 3
  • 29
  • 68

3 Answers3

0

modules located in same location should be imported without any issue

surendra
  • 11
  • 2
0
import sys
sys.path.insert(0,'projectname/scr')
from test1 import anything

I think, this helps you. By inserting this path to avaliable paths at index 0, you might easily call your module.

0

Resolved the issue by marking the src as root sources inspired by this StackOverflow question. I still do not understand why it happened and why setting this directory as root source resolve this issue but it works for now.

utengr
  • 3,225
  • 3
  • 29
  • 68