Search code examples
pythonimportmodulevirtualenvvirtual-environment

Python- No module named "src"


I am trying to import my module name "src" into the "data_ingestion.py" file but the VS code, terminal showing this error "No module named src" Why VS code is not detecting my module?

The pics of the problem are given:

enter image description here

Is it a problem with the virtual environment? OR it is a Run time error?

I linked the file with ".venv" by going to settings>select interpreter>python 3.11.8(".venv":venv).I thought that maybe".venv" was not linked to the data_ingestion.py file. If this is the problem then please tell me how can I solve this.

I have also tried giving the full file path to the VS code terminal but that also does not work.


Solution

    File, folder structure,

    parentfolder
      code.py
      folder 
        script.py 
    

    In python, to import your scripts, functions you should use relative paths or absolute paths. So, when you import like,

    from folder.script import function 
    

    The folder should be sub folder of where the script is.

    ----------------------------------------

    2)

    File, folder structure,

    parentfolder
      script.py 
      folder 
        code.py
    

    So, maybe try like below.

    from .folder.script import function 
    

    Which is import "function" from a "script.py" file from parent folder called "folder".