Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 98472

VSCode: How to import a module from my code path to another file in the same path?

$
0
0

I'm trying to debug my python app using VSCode. But I cannot configure my environment right. Attempting to import a class from one folder in my source path to another, gives me this message:

Traceback (most recent call last):
  File "/Users/mb/Library/source/sandbox/lib/lib.py", line 1, in <module>
    from app.main import MyClass
ModuleNotFoundError: No module named 'app'

I created a simple app to exhibit the problem. My source path looks like this:

sandbox
+-- .vscode
    --- launch.json
+-- app
    --- __init__.py
    --- main.py
+-- lib
    -- lib.py
# main.py

class MyClass:
    def __init__(self):
        print('Creating object...')
    def print_message(self, message):
        print(message)
# lib.py

from app.main import MyClass


myclass = MyClass()
myclass.print_message('hello, world!')

Trying to run lib.py using the default configuration to run current file I get the error message above.

Additionally, I created a .vscode/launch.json to set the working directory, but to no avail. Here is my 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": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${workspaceFolder}"
        },
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "main"
        }
    ]
}

I'm sure, I'm missing a simple setup, but I can't put my finger on it.


Note

  • The interpreter used is: Python 3.7.5 64-bit
  • The above code works fine in PyCharm.

Viewing all articles
Browse latest Browse all 98472

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>