2

Although there are multiple posts on stack overflow about custom module not found error, but those solutions (such as this one) didn't help in my case.

My folder structure is like this:

project
|-- app
|   |-- script.py
|-- src
|   |-- __init__.py
|   |-- file.py

In my script.py I have this code

import pandas as pd
import streamlit as st
from project.src.file import func

Using VS Code in a windows7 machine, I changed the directory to project, and activated a conda virtual environment, then executed the code streamlit run app\script.py, I got this error

ModuleNotFoundError: No module named 'project'

Changing the script to from src.file import func caused this error

ModuleNotFoundError: No module named 'src'

Could someone please explain what happened? Why Python couldn't find my custom module even though I had the required __init__.py in the folder src.

Nemo
  • 1,124
  • 2
  • 16
  • 39
  • If it's not windows, you'll want to run app/script.py (forward-)slash, not backslash. If your current directory is project, then your module is src.file, not project.src.file. And project is not a package because there's no _ _ init _ _.py in it, only in its subdir. – Yuri Feldman Oct 26 '21 at 14:27
  • @YuriFeldman, my machine is a windows 7. Changing to `from src.file import func` didn't work unfortunately. – Nemo Oct 26 '21 at 14:56
  • Then I would check that file.py contains a func and that your current dir is indeed project (import os; print(os.getcwd()) before the import). The latter is actually the prime suspect. As with any other programming proejct I would also make sure that its path (all the way from partition root) does not contain non-ASCII characters (e.g. your username), and also not spaces. – Yuri Feldman Oct 27 '21 at 05:45

2 Answers2

3

You need to change the import in script.py to

from src.file import func

and run it in the project directory using

python -m app.script

Since you're using streamlit and apparently it doesn't support running modules, you can try the method provided in this Github issue:

  1. Create a file named run.py inside your project directory with the following contents:
import runpy

runpy.run_module("app.script", run_name="__main__", alter_sys=True)
  1. Run the streamlit command:
streamlit run run.py
enzo
  • 9,861
  • 3
  • 15
  • 38
  • I have to run the `script.py` using command `streamlit run...`. When I tried `streamlit run -m app.script` as per your suggestion, I got this error `no such option: -m` – Nemo Oct 26 '21 at 14:53
  • Check my updated answer. – enzo Oct 26 '21 at 14:57
  • Thanks enzo. I'll try your suggestion later but my main question was why Python couldn't find my custom module although I have the required `__init__.py` in the `src` folder. – Nemo Oct 26 '21 at 15:02
  • 1
    This is not related with Python, but with the streamlit library. You're structuring your project in modules, therefore it should be run using `python -m`. Since streamlit only allows to run a single file, the import error appears. – enzo Oct 26 '21 at 15:06
  • Could you please elaborate what you meant by a single file? The script also import 2 other "files" (pandas and streamlit) but the error only happened with my custom module. – Nemo Oct 26 '21 at 15:14
  • When you run `streamlit run file.py`, you're running a single file called *file.py*. When you run `python -m some.module`, you're running a single module called *some.module*. streamlit [can't run modules yet](https://github.com/streamlit/streamlit/issues/662) (i.e. you can't run `streamlit run -m some.module` as you've already tried), only files. – enzo Oct 26 '21 at 15:29
  • Given that your project is structured in modules and streamlit can't run modules, streamlit can't run your custom module and gives the import error you received. This is not a issue with Python: if you run `python -m app.script` as the first part of my answer describes, you'll see that the module is run successfully. Since I'm assuming you need to use streamlit, you must use a workaround, as the second part of my answer describes. – enzo Oct 26 '21 at 15:33
1

Turned out that the python interpreter in my VS Code didn't know the path of my project folder.

Following this suggestion, I set the PYTHONPATH in my VS Code as follows:

set PYTHONPATH=%PYTHONPATH%;path\to\the\folder\project

with the code in script.py as from src.file import func.

the command streamlit run app\script.py then worked without errors.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Nemo
  • 1,124
  • 2
  • 16
  • 39