2

I have a certain project structure:

 - azima
   - .vscode
   - core
     - project_setup.py
   - helper
     - log_helper
   - venv

In project_setup.py:

import os
import json
import numpy as np
import pandas as pd
import random
from helper.log_helper import log

if __name__ == "__main__":
    print('hello world')

Running this file in terminal:

(venv) rmali@rakeshmali:~/git/azima$ /home/rmali/git/azima/venv/bin/python /home/rmali/git/azima/core/project_setup.py
Traceback (most recent call last):
  File "/home/rmali/git/azima/core/project_setup.py", line 6, in <module>
    from helper.log_helper import log
ModuleNotFoundError: No module named 'helper'

I get this error. What am I doing wrong? Am I missing something?

But running like this python -m core.project_setup works.

Azima
  • 3,835
  • 15
  • 49
  • 95
  • Have you tried `from .helper.log_helper import log`? – Sylvester Kruin Sep 26 '21 at 15:01
  • @SamMatzko `ImportError: attempted relative import with no known parent package` I get this error – Azima Sep 26 '21 at 15:05
  • Then use your import method (`from helper.log_helper import log`), but before it put `sys.path.append(os.path.dirname(os.path.dirname(__file__)))`. This should add `azima` to the list of paths where Python searches for modules. – Sylvester Kruin Sep 26 '21 at 15:19
  • Do you have `__init__.py` scripts in your directory structure? These would tell Python that you to treat the folders as packages and subpackages. – ogdenkev Sep 27 '21 at 06:18
  • Might be related to https://stackoverflow.com/questions/40185437/no-module-named-numpy-visual-studio-code if you have multiple python installations and VSCode has selected a default python interpreter that is not the one where you have installed the module you're trying to import – jlo Oct 17 '21 at 15:28
  • This solution worked for me: https://stackoverflow.com/a/73567911/4347611 – Binny Aug 27 '23 at 04:59

2 Answers2

2

Reason:

The path of folder azima does not in the sys.path(PYTHONPATH).

Solution:

You can do this to modify the PYTHONPATH:

  1. Add these in the settings.json file to Modify the PYTHONPATH in the terminal:

    "terminal.integrated.env.windows": { "PYTHONPATH": "xxx/site-packages" }

  2. Create a .env file under your workspace, and add these settings in it to modify the PYTHONPATH for the extension and debugger: PYTHONPATH=xxx/site-packages

You can refer to here to understand the effects of these two configurations.

  1. Modify it directly in the python file. Add these codes in the b.py file.

    import sys; sys.path.append("xxx/Project/src")

Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
1

The reason that running

(venv) rmali@rakeshmali:~/git/azima$ python ./core/project_setup.py

fails while

(venv) rmali@rakeshmali:~/git/azima$ python -m core.project_setup

succeeds is that when running python -m <module-name, Python adds the current directory to the start of sys.path, which allows modules in that directory such as helper to be imported as top level modules, i.e. with import helper. Running python <script> does not add the current directory to the start of sys.path. Instead, Python adds the directory containing the script to the start of sys.path.

Here are the relevant sections of the docs.

The -m switch

As with the -c option, the current directory will be added to the start of sys.path.

and the docs for the -c option add

the current directory will be added to the start of sys.path (allowing modules in that directory to be imported as top level modules)

Docs for python <script>

If the script name refers directly to a Python file, the directory containing that file is added to the start of sys.path, and the file is executed as the __main__ module.

If the script name refers to a directory or zipfile, the script name is added to the start of sys.path and the __main__.py file in that location is executed as the __main__ module.

ogdenkev
  • 2,264
  • 1
  • 10
  • 19