3

The project structure is like

- a.py
- unittest
   - test_a.py

A.py

class A():
    def function_a(self):
        return 'function_a'

test_a.py

from a import A

class TestA():
    def test_a(self):
        assert A().function_a() == 'function_a'

When I use terminal and set PATHONPATH=//<my-project-folder>, it can be detected and tested via pytest. However, when I tried to debug it in VScode, it kept throwing out No module named a.

https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file

I've tried this way and put a .env inside my project folder.

.env

PYTHONPATH=C:\\Repos\\test-project

(I did it with and without escaping, it didn't work anyway.)

.vscode/setting.json

{
    "python.pythonPath": "C:\\Python27\\python.exe",
    "python.testing.pytestArgs": [
        "unittest"
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.envFile": "${workspaceFolder}/.env",
}

1 Answer 1

5

To find out what the value is of the PYTHONPATH environment variable use the following test_a.py file

import os

def test_PYTHONPATH():
  assert os.environ.get('PYTHONPATH') == "XX"

I have tested it on Windows with Python 3.6.

In my test I have set an .env in the workspace root folder with the content

PYTHONPATH=C:\Projects\test-project\

Running the test gave an assertion error and showed that the value of PYTHONPATH was the value defined by the OS.

Starting a terminal and removing PYTHONPATH from the global environment variables and starting VSC from this terminal with C:\Projects\test-project\ as argument opened the project and now the test fails but shows the correct PYTHONPATH as defined in the .env file.

I get the conclusion that the OS environment variables (on Windows) override any environment variable set by VSC. If this is correct you can't extend the environment variable like it is shown in the docs

PYTHONPATH=${PROJ_DIR}:${PYTHONPATH}

This should be filed as an error in VSC or the VSC-python extension.

Anything defined in VSC should override any OS environment variable and be able to use any OS environment variable.

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

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.