ERPNext dev environment error on Docker - "No module named 'frappe'"

I am setting up ERPNext dev environment on Docker based on the documention and I cannot get it to debug. When I debug it thorws an error “No module named ‘frappe’”. Tried reinstalling this many times and no success. This is on a Ubuntu 20.04.

Documentation followed : frappe_docker/development at main · frappe/frappe_docker · GitHub

I have done this before on same Ubuntu version and did not encounter any errors. Following is the error I am getting and please help me fix it.

Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/frappe/.vscode-server/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
    cli.main()
  File "/home/frappe/.vscode-server/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 444, in main
    run()
  File "/home/frappe/.vscode-server/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "/usr/lib/python3.7/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/usr/lib/python3.7/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/workspace/development/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 3, in <module>
    import frappe
ModuleNotFoundError: No module named 'frappe'
$  cd /workspace/development/frappe-bench/sites ; /usr/bin/env /usr/bin/python3 /home/frappe/.vscode-server/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/launcher 44473 -- /workspace/development/frappe-bench/apps/frappe/frappe/utils/bench_helper.py frappe worker --queue long 
$  cd /workspace/development/frappe-bench/sites ; /usr/bin/env /usr/bin/python3 /home/frappe/.vscode-server/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/launcher 34775 -- /workspace/development/frappe-bench/apps/frappe/frappe/utils/bench_helper.py frappe serve --port 8000 --noreload --nothreading

what are the contents of .vscode directory? tasks.json, launch.json

It has launch.json and settings.json. No tasks.json. I just did a fresh installation deleting all the containers and still getting the same error. I tried two options for running this in debug mode.

Contents of the files are
settings.json

    "python.pythonPath": "frappe-bench/env/bin/python",
    "debug.node.autoAttach": "disabled"
} 

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Bench Web",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/frappe-bench/apps/frappe/frappe/utils/bench_helper.py",
            "args": [
                "frappe", "serve", "--port", "8000", "--noreload", "--nothreading"
            ],
            "cwd": "${workspaceFolder}/frappe-bench/sites",
            "env": {
                "DEV_SERVER": "1"
            }
        },
        {
            "name": "Bench Default Worker",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/frappe-bench/apps/frappe/frappe/utils/bench_helper.py",
            "args": [
                "frappe", "worker", "--queue", "default"
            ],
            "cwd": "${workspaceFolder}/frappe-bench/sites",
            "env": {
                "DEV_SERVER": "1"
            }
        },
        {
            "name": "Bench Short Worker",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/frappe-bench/apps/frappe/frappe/utils/bench_helper.py",
            "args": [
                "frappe", "worker", "--queue", "short"
            ],
            "cwd": "${workspaceFolder}/frappe-bench/sites",
            "env": {
                "DEV_SERVER": "1"
            }
        },
        {
            "name": "Bench Long Worker",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/frappe-bench/apps/frappe/frappe/utils/bench_helper.py",
            "args": [
                "frappe", "worker", "--queue", "long"
            ],
            "cwd": "${workspaceFolder}/frappe-bench/sites",
            "env": {
                "DEV_SERVER": "1"
            }
        },
        {
            "name": "Honcho SocketIO Watch Schedule Worker",
            "type": "python",
            "request": "launch",
            "program": "/home/frappe/.local/bin/honcho",
            "cwd": "${workspaceFolder}/frappe-bench",
            "console": "internalConsole",
            "args": [
                "start", "socketio", "watch", "schedule", "worker_short", "worker_long", "worker_default"
            ]
        }
    ]
}

make sure :

  1. Your Procfile should not have line for web: ... comment it with #
  2. The python extension is available in devcontainer. Install it if necessary.

I’ve updated the repo with updates for devcontainer to use bash as default shell, instead of sh as default.

No luck. Commented the Procfile line starting #web and uninstalled and installed the python extension on the container. Still the same error.

I don’t know, I just tried locally from scratch, cleaned up everything and tried, even cloned the repo again. Things are working on my Linux machine. I can’t help much.

Thanks a lot for your help! I tried from scratch many times and for some reason, it doesn’t work. I have another laptop and it is working there. I might have to do a comparison and see. (Done already but will take a deeper look)

I think I have some clue now. Compared both laptops and when I print the sys.path on the laptop where debug is working I see ‘frappe’ and ‘erpnext’ paths are in the sys.path.

On the laptop where debug is not working frappe and erpnext aren’t there in the sys.path.

I just checked the file easy.install.pth in env folder and both frappe and erpnext paths are there but not loading. Added a new file my-paths.pth with frappe and erpnext paths under the folder env/…/site-packages folder but no luck. I am not seeing paths loaded. Could you please help me fix this path issue?

Below is the content of sys.path where debug is not working.

['/workspace/development/frappe-bench/apps/frappe/frappe/utils', 
'/usr/lib/python37.zip', '/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload', 
'/home/frappe/.local/lib/python3.7/site-packages', 
'/home/frappe/.bench',
'/usr/local/lib/python3.7/dist-packages',
'/usr/lib/python3/dist-packages']

Below is the content of sys.path where debug is working.

['/workspace/development/frappe-bench/apps/frappe/frappe/utils',
'/workspace/development/frappe-bench/env/lib/python37.zip',
'/workspace/development/frappe-bench/env/lib/python3.7',
'/workspace/development/frappe-bench/env/lib/python3.7/lib-dynload',
'/usr/lib/python3.7',
'/workspace/development/frappe-bench/env/lib/python3.7/site-packages',
'/workspace/development/frappe-bench/apps/frappe',
'/workspace/development/frappe-bench/apps/erpnext']

The reason for this issue is that it is not picking up paths properly. I just edited the bench_helper.py and added the following lines and it started in the debug mode without errors.

I am not a Python expert and if someone could guide me fixing this issue in a proper way would be highly appreciated.

import sys
sys.path.insert(0, "/workspace/development/frappe-bench/apps/frappe")
sys.path.insert(0, "/workspace/development/frappe-bench/apps/erpnext")
sys.path.insert(0, "/workspace/development/frappe-bench/env/lib/python37.zip")
sys.path.insert(0, "/workspace/development/frappe-bench/env/lib/python3.7")
sys.path.insert(0, "/workspace/development/frappe-bench/env/lib/python3.7/lib-dynload")
sys.path.insert(0, "/usr/lib/python3.7")
sys.path.insert(0, "/workspace/development/frappe-bench/env/lib/python3.7/site-packages")

Added the PYTHONPATH to ENV section of the launch.json for now. Hope someone would figure this out and let us know a fix.

            "env": {
                "DEV_SERVER": "1",
                "PYTHONPATH": "/workspace/development/frappe-bench/apps/frappe:/workspace/development/frappe-bench/apps/erpnext:/workspace/development/frappe-bench/env/lib/python37.zip:/workspace/development/frappe-bench/env/lib/python3.7:/workspace/development/frappe-bench/env/lib/python3.7/lib-dynload:/usr/lib/python3.7:/workspace/development/frappe-bench/env/lib/python3.7/site-packages"
            }
1 Like