-1

Sorry if this a dumb question but i could not find similar question like this, Can someone help me understand the absolute import and how importing actually works in python, I watched a video where the instructor created two package one called package1 and package2, in the package2 this has a sub_package since am using python 3 i recreated the video topic and the above I placed an init file to some of the packages but not all because init is not really needed in the first place. I also have a root_file which is in the main directory of the overall project top level, this root_file module or script can access all the package, now when I import all the module via their package and sub-package into the root_file I have a function and name in the module to be printed out so when called from root_file.py I know who is who, this work fine, however, my issue is now when I try to absolute import from (package2 / module name = file2) into (package1 / module name = file1) I get a dirty big fat error

ModuleNotFoundError: No module named 'package2'
Traceback (most recent call last):
    from package2 import file2
ModuleNotFoundError: No module named 'package2'

However, the import above worked for the instructor. also, the same error occurs when I even attempt the basic to import from same package1 file1 into package1 file 2 I get

Traceback (most recent call last):
  File "/Users/ganiyu/Desktop/Python_import/package1/file1.py", line 2, in <module>
    from package1.file2 import pkg_1_file_2_func
ModuleNotFoundError: No module named 'package1'

Why does it only work when I import call from the packages and module into root_File.py or can some try recreating my issue or give me my answer also i am lost on why python keeps saying that 'package2 ' or the i try with package one or sub_packed it says the packages are modules.

enter image description here enter image description here

EDITED

I found out that vscode is the issue it work on pycharm very all import combination can someone assist me how to get this working on vscode because am lost.

enter image description here

ganja
  • 21
  • 3

2 Answers2

0

Make sure package2 is in the same directory as your file1.py. From just looking at your directories, you can just move the package2 folder into your package1 folder.

  • package1
    • package2
      • package2 files
    • file1.py
Landon
  • 119
  • 11
  • this does not work with when i then try calling package1 friles from package2 file i get module error, python say my module cant be found which – ganja Aug 15 '20 at 22:26
  • I just found out that vscode has issue basically I wasted a whole day thinking am crazy i tried the allt ]he combination on pycharm and it works the example i showed above work in pycharm and not vs code. – ganja Aug 15 '20 at 22:49
0

When you run Python scripts, the current directory comes first for looking for modules, but the PYTHONPATH is also used.

It appears that your PYTHONPATH is not being set when you use VS Code, and VS Code is running the script from its own directory. PyCharm must be either setting it by default to the project root, or running the script from the project root. Please refer to How to use PYTHONPATH with VSCode Python Extension for Debugging?

ELinda
  • 2,658
  • 1
  • 10
  • 9