PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 5, 2022

[FIXED] How to modify environment variables (such as PATH) when using python.unittest in VS Code?

 November 05, 2022     environment-variables, python, unit-testing, visual-studio-code     No comments   

Issue

I'm writing a python unit test using unittest and I want to compile some Solidity contracts in the test setup.

However, when I did the compile by os.system() or subprocess.run(), it shows that solc, the Solidity compiler, not found. While it runs properly when running in a non-test python program.

After this happens, I found a further interesting things: when I print(os.environ) in both a test python program using unittest and a normal python program, the result is of hugh difference! Including the most important one: $PATH. It looks like the following:

****PATH in unittest****
/usr/bin
/usr/local/bin
...(mostly the default $PATH set by Linux)


****PATH in normal program****
/usr/bin
/.../myEnvs/.../bin
...(as same as my console's $PATH, which is exported in .bashrc)

Since I'm working with others to develop this program, I should NOT add path such as "myEnvs" in program (by using environment setting like env= parameter provided by subprocess.Popen()).

I think the abnormal $PATH in python.unittest is caused by some configurations introduced by VScode, so what are these configs? How could I modify them? or this "PATH inconsistence" is caused by some other reasons?


Solution

Okey..... After a period of time, I finally find out what happens:

  1. Exactly, The so-called "$PATH difference" is caused by VScode configurations in launch.json.
  2. You can create a new term like this and make your unittest configuration same as a normal debug:
{
    ...
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal", 
            "justMyCode": false,
            "args": []
        },
        {
            "name": "Python: Debug Tests",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "purpose": ["debug-test"],
            "console": "integratedTerminal", // Consistence with ```Python: CurrentFile```
            "justMyCode": false,
        },
    ]
}

  1. This Debug Test configuration can be used in different test mode, see more in VSCode doc - Debug tests.

  2. The critial one is "console", the default value is "internalConsole", which seems to share the same env with system defaults. While I should set it to "integratedTerminal", which is consistent with my own shell.



Answered By - Alexious
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing