0

Given a project structure:

* project
  * src
    * my_package
      * __init__.py
      * code_file.py
  * tests
    * __init__.py
    * my_package
      * __init__.py
      * code_file_test.py

How do I get Visual Studio code to identify that src is the root for code files so this will work for all of these:

  1. intellisense

  2. running

  3. discovering and running tests

1 Answer 1

0

In IntelliJ so can right click the src folder and mark it as the source root. There is no simple solution in VSC, however, this workaround could help.

Under the project root add the following file:

.vscode/settings.json

* project
  * .vscode
    * settings.json

with the following content:

{
    "python.analysis.extraPaths": ["${workspaceFolder}/src/", "${workspaceFolder}/"],
    "python.autoComplete.extraPaths": ["${workspaceFolder}/src/", "${workspaceFolder}/"],
    "terminal.integrated.env.windows": {
        "PYTHONPATH": "${workspaceFolder}\\src;${workspaceFolder}"
    },
    "terminal.integrated.env.linux": {
        "PYTHONPATH": "${workspaceFolder}/src:${workspaceFolder}"
    },
    "python.testing.unittestArgs": [
        "-v",
        "-t", ".",
        "-s", "./tests",
        "-p", "*test.py"
    ],
    "pythonTestExplorer.testFramework": "unittest",
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": true
}

(The two last parameters assume you are using the unittest python framework, change if using another one.)

If this doesn't work, make sure you have the following extensions installed:

ms-python.python
ms-python.vscode-pylance
ms-vscode.test-adapter-converter
hbenl.vscode-test-explorer
littlefoxteam.vscode-python-test-adapter

If the above doesn't work, try restarting VSC, if that doesn't work, you may have to alter imports like this...

In src code:

# Instead of: from my_package.different_file import AnotherClass
# use:
from .different_file import AnotherClass

# Instead of: from my_other_package.different_file import AnotherClass
# use:
from ..my_other_package.different_file import AnotherClass

In tests code:

try:
    from my_package.my_file1 import MyClass1
    from my_package.my_file2 import MyClass2
except ImportError:
    from src.my_package.my_file1 import MyClass1
    from src.my_package.my_file2 import MyClass2
    pass
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Danny Varod, glad to know you've found the solution to resolve this issue! you could click '✔' to mark it as an answer to change its status to Answered. It will also help others to solve a similar issue. See also can I answer my own question.., Just a reminder :)
@MingJie-MSFT this mostly works, hoping for a better solution. I've edited the answer accordingly. I'm pretty sure the only way to completely support this is add support to VSC itself.
If you try to open src directly as a workspace does that meet your need?
@MingJie-MSFT, no. The repo contains src and tests and requirements.txt and other files, I need the entire repo. In IntelliJ and PyCharm this works via Mark directory as Sources root. Which sets PYTHONPATH env variable as I did above for the terminal for everything running in that instance of IJ/PY including terminal, intellisense, tests, run, debug etc. This is the main reason I prefer IJ over VSC despite VSC's many options.

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.