0

I have this project structure:

/project_name
    main.py
    ----- __init__.py
    ------ /modules
    -------- __init__.py
    -------- module1.py
    -------- module2.py

I've edited to add more information. After working and testing a lot of recomendations to solve the problem, nothing works.

Enviroment

  • Windows
  • Conda virtual enviroment project python 3.10
  • VSCode

Problem

When running main.py from VScode

from modules.module1 import *

if __name__ == "__main__":
    pass

this error raise

from module1 import *
ModuleNotFoundError: No module named 'module2'

Modules

module1.py

from module2 import *

module2.py

def test():
    print("just testing")

So the problem always occurs when from main.py i import a module that imports another module. The second module imported from the module that i have imported from main.py is not found.

Still looking for the solution

Vince
  • 507
  • 8
  • 21
  • 2
    You're probably running/your script from the wrong directory. Are you running things from `project_name/`? Or did you set PYTHONPATH correctly? Or use an editable installation (`pip install -e .`)? – 9769953 Jan 30 '23 at 10:04
  • 2
    You can also use relative imports: `from .module1 import method1` in `module2.py`. But this won't really solve the problem when running things from the wrong directory. – 9769953 Jan 30 '23 at 10:05
  • 3
    As an aside, `src` should almost certainly not form part of your package name. You should configure your project to search packages *inside* `src/`, so that you can then write `import data.module1` instead. Using Poetry this is especially easy by adding a line `packages = [{include = "data", from = "src"}]` into your `pyproject.toml` file. – Konrad Rudolph Jan 30 '23 at 10:08
  • `src` is acting as a package here. So probably you should run `module2.py` as: `python -m src.modules.module2` (Should run the line at dir: /project_name) – SimZhou Jan 30 '23 at 10:10
  • So, you mean that src folder is useless, isn't it? just move data back 1 level and remove src folder?. Many thanks. – Vince Jan 30 '23 at 13:23
  • As my enviroment is Windows, Conda, VScode, i can't the solution. Many different proposed solutions. I keep trying... – Vince Jan 30 '23 at 18:18

5 Answers5

0
  1. You could try to set PythonPath first. If you use vscode to develop, you could set this PythonPath in setting.json
  2. if module1.py and module2.py are in the same directory, you could try to use relative import. Please pay attention to cycle import as well.
from .module1 import Module1
  1. main.py had better to move into src directory.
KennetsuR
  • 704
  • 8
  • 17
0

run the file from project_name folder

If you run a file from inside a folder, python will be able to look local module from this folder. You need to call it at the root of the project.

user/projects/package_name$ python -m src/modules/module2.py
Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
0

Pycharm working directory

If you use Pycharm, you can configure the run configuration working directory to the root of your project.

configuration panel of pycharm

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
0

Import your package in editable mode

Create a pyproject.toml file in the root of the project with this content:

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "package_name"
version = "0.0.1"
requires-python = ">=3.7"

Your package need to follow this structure:

project_name/
└── src/
    └── package_name/
        ├── __init__.py
        └── example.py

To install it in your environment from the root of the project:

user/projects/package_name$ pip install -e .

By doing that, you won't have to worry about PYTHONPATH, workind directory, relative/absolute import. You just import and use your package using the intended path you created, Python will know how to look thanks of the pip install command.

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
0

Thanks all. Finally i've solved it including this line before importing my file module:

sys.path.append("X:\\path\\root_folder\\")
Vince
  • 507
  • 8
  • 21