3

My project structure is something like this:

- my_pkg
    setup.py
    README.md
    - my_pkg
        __init__.py
        __main__.py
         - src
             app.py
             part.py
             __init__.py
         - tests
             test_app.py
             test_parts.py
             __init__.py

In test_app.py I have the following import statement:

import my_pkg.src.app as app

In my terminal I can run the file using

python -m my_pkg.tests.test_app

This runs fine without any errors, but when I right click on test_app.py and choose "Run Python File in Terminal" I get the following error:

ModuleNotFoundError: No module named 'my_pkg'

I have installed my_pkg by running:

pip install -e .

If I open up a terminal and run python and in python run "import my_pkg.src.app as app" it works fine.

What am I doing wrong. How can I get my imports to work when running my program in visual studio code?

3 Answers 3

3

I was able to find a way to get the debugger to work by changing the launch.json file:

{
    "version": "0.1.0",
    "configurations": [
        {
            "name": "Python: Module: my_pkg",
            "type": "python",
            "request": "launch",
            "module": "my_pkg",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "env" : {"PYTHONPATH": "${workspaceFolder}"},
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        },
        {
            "name": "Python: Current File (External Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "env" : {"PYTHONPATH": "${workspaceFolder}"},
            "console": "externalTerminal"
        }
    ]
}

The "Python: Module my_pkg" will run my module by running the __ main __.py-file with a -m argument, and the "Python: Current File (Integrated Terminal)" and "Python: Current File (External Terminal)" runs the current file open, but gives workspaceFolder as PYTHONPATH so my imports don't break.

I have still not found a way to change the configuration so that I can right click on a file and choose "Run Python File in Terminal" without it breaking. But I just run it in the terminal manually until I find a solution to this.

Sign up to request clarification or add additional context in comments.

Comments

0

Change your directory to 'my_pkg' and run your code as follows

python -m my_pkg.tests.test_app

Check out the -m flag documentation here

1 Comment

Yes, I am able to run it like this from the terminal. How can I configure VS code to run it automatically like this in debug mode, when running in terminal, and when running automatically detected tests?
0

Because your running cwd is at your "test.py" file.

You need to add root dir to your system path

import sys
import os
sys.path.append(os.path.join(os.path.dir(__file__), "from/file/to/root"))
print (sys.path)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.