dagrun
dagrun

Reputation: 651

How to import from local python packages in VS Code?

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?

Upvotes: 3

Views: 10107

Answers (3)

dagrun
dagrun

Reputation: 651

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.

Upvotes: 3

user6172474
user6172474

Reputation:

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)

Upvotes: 0

Parth Sharma
Parth Sharma

Reputation: 105

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

Upvotes: 0

Related Questions