0

I know there are dozens of Python package import questions, but I can't seem to find an answer that works. Most answers seem to be about the missing __init__.py to make a package, but I've got one in the base of the project (and I've tried in the sub-folders as well).

I have a project of the form

package
+-- __init__.py
+-- weasel.py
+-- badgers
|   +-- __init__.py
|   +-- a.py

where weasel.py just contains a test function

def test_function():
    return "HELLO"

and I want to be able to call test_function from a.py, I've attempted (all with and without the init.py in the folder badgers) and none of which seem to work.

import weasel -> ModuleNotFoundError: No module named 'weasel'
from . import weasel -> ImportError: attempted relative import with no known parent package
import package.weasel -> ModuleNotFoundError: No module named 'package'
from package import weasel

The hack I've managed to employ thus far, which works fine in Spyder and in my production environment; project is a Dash App (so Flask) deployed to render.

import sys
sys.path.append("..")
import weasel

But this just throws ModuleNotFoundError in VS Code.

I'm not adverse to a hack :S but the needs of the current project kinda make life much, much, easier if I could build the project in VS Code.

Please, I implore the stackoverflow community could someone please let me know what I'm doing wrong? Or a hack that will work in VS Code?

Thank you,

1 Answers1

0

This is caused by the path. The path of vscode running file is not the current file path. You need to add it manually.

import sys
sys.path.append("your package's Path")  #for example, my path is "C:/my_python/package"
import weasel

Here is my directory structure.

enter image description here

Tips:

Do not use "..", it will really adds two commas to path instead of parent directory.

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
  • Thank you very much! Playing around I seem to be able to build a function that automatically adds the parent to the path using os.path.reakpath('__file__') and parents, which has solved the problem (and seems to generalise). Are you able to explain why just having __init__.py doesn't make it available like most tutorials seems to suggest? Thank you again... this has been confusing my for longer than I'd like to admit. – pontificating_panda Apr 29 '22 at 07:35
  • @pontificating_panda In fact, the method we use is similar. The reason for this problem is that the vscode running file is based on the workbench directory, not the directory where the file is located. You can use the following method to verify: import os print(os.listdir()) – MingJie-MSFT Apr 29 '22 at 09:05
  • And python will recognize ".." as a string instead of a parent directory. You could use the following code to verify: import sys print(sys.path) sys.path.append("..") print(sys.path) – MingJie-MSFT Apr 29 '22 at 09:14
  • thank you very much for the help. Genuinely much appreciated – pontificating_panda Apr 29 '22 at 11:46