1

This is my directory structure.

PyCharm
    Python
        PRD
            _AppFunc
                DbUtils.py
                __init__.py

            _AppCode
                DataExtract.py
                __init__.py

            __init__.py

        __init__.py

And my PYTHONPATH is set to C:\Users\username\Desktop\PyCharm\Python\PRD

In DataExtract.py I am trying to import DbUtils.py using

from .._AppFunc import DbUtils

And getting this error...

Traceback (most recent call last):  
File "C:\Users\username\Desktop\PyCharm\Python\PRD\_AppCode\DataExtract.py", line 36, in <module> from .._AppFunc import DbUtils   
ValueError: Attempted relative import in non-package

If I move DBUtils.py out of _AppFunc folder and under PRD, it works fine. But I intend to keep it in a separate folder to restrict access.

I have read most stack overflow threads related to this and python doc on intra-package-references but couldn't get it to run.

How do I go about it !?

WeShall
  • 409
  • 7
  • 20

1 Answers1

0

Here is what worked...

  1. The PYTHONPATH should be set to the root. For the directory structure in question it is C:\Users\username\Desktop\PyCharm
  2. Each folder and sub-folder in the structure starting from root should be made a package. Copy paste __init__.py and __init__.pyc
  3. The module is imported in the main file by a full path reference. For this question it is Python.PRD._AppFunc import DbUtils
WeShall
  • 409
  • 7
  • 20