1

I use VS Code for Python and I have the following file structure:

--src

----transform

------text

--------text.py

----transform.py

So as you can see, I have a src file, inside I have transform file and inside transform there's a text file that contains text.py python file with class Text. Meanwhile, inside the transform file there's a transform.py python file. Inside this transform.py file I import text as from src.transform.text.text import Text. This was auto-imported from vsc so I didn't practically did it and it doesn't show any errors when I test it locally. However, when deployed, I get the error ModuleNotFoundError: No module named src.. Regarding this, I have multiple questions:

  1. Why is the vsc code importing not correct?
  2. Can you suggest some vsc code python importer?
  3. How can I fix this, i.e is there some convention to follow regarding this? It works on my computer joke is present here, but deployed it throws an error.
0

1 Answer 1

1

You need a file named __init__.py in each directory you want python to scan. Can you confirm this is present? You don't need a specific python importer in vscode as the module structure is defined by the folder structure of the source files.

If your src folder is not the root folder vscode has open, you may need to update the python.autoComplete.extraPaths and python.analysis.extraPaths fields in settings to something like "${workspaceFolder}/python":

{
    "python.autoComplete.extraPaths": [
        "${workspaceFolder}/python",
    ],
    "python.analysis.extraPaths": [
        "${workspaceFolder}/python"
    ]
}

Sign up to request clarification or add additional context in comments.

2 Comments

No, I don't have an __init__.py file. Should I type something in this file or I just need to create it?
the file can be empty. typically it is just used to let python know it should scan the directory; however, you can put code in there as well. any functions, classes etc in your src/__init__.py would be accessed as from src import function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.