How to write code that only runs when debugging python in visual studio

Asked 2 years ago, Updated 2 years ago, 64 views

Is there a way to write code that only runs when debugging python in visual studio?
With c++, I can use macros...

Environment
visualstudio 2019
python 3.6

python visual-studio

2022-09-30 17:56

2 Answers

https://stackoverflow.com/a/27455446/1979953

Your answers will be helpful.

quote
Use __debug__ in your code:

 if__debug__:
    print 'Debug ON'
else:
    print 'Debug OFF'

Create a script abc.py with the above code and then

Observe the difference.

translating quotes
You can use __debug__.

 if__debug__:
    print 'Debug ON'
else:
    print 'Debug OFF'

Create the above code under the name abc.py.

You can see the difference by running it on the .


2022-09-30 17:56

For now, @shingo.nakanishi, the answer method is probably the one that has the least impact on workload and performance.

__debug__

This constant is true unless Python started with the -O option enabled. See also the assert statement.

Annotation:Since the names None, False, True, and __debug__ cannot be reprinted (replies for these will send SyntaxError even as attribute names), these are considered 考えられますtrue 定数 constants.

-O

Delete the assert statement and code based on the value of __debug__. Extend the filename of the compiled (byte code) file by adding .opt-1 before the .pyc extension (see PEP488). See also PYTHONOPTIMIZE.

-OO

-O and discard docstring. Extend the filename of the compiled (byte code) file by adding .opt-2 before the .pyc extension (see PEP488).

PYTHONOPTIMIZE

Setting a non-empty string for this variable is equivalent to specifying the -O option.If you set an integer, it is the same as specifying -O multiple times.

However, without the optimization option and debugging mode do not seem to match the exact meaning of the word or to be very chic.

If you forget to add options at runtime, you will also be operating in debug mode by default.

Therefore, Visual Studio is considered to have similar effects by specifying your own defined options and environment variables in the Script Arguments or Environment Variables column of the project properties.
C++ macros are also compiled by changing the definition settings in these project properties, although Python is compiled into byte code, largely because it is an interpreter classification.

Debugging Python Codes
Project debug options

By default, the debugger starts the program using a standard Python launcher.It does not use command-line arguments or special paths or conditions. You can change startup options in the debug properties of the project.To access debug properties, right-click the project in Solution Explorer, select Properties, and select the Debug tab.

Quotes from above page

VisualStudioPythonDebug

You will be able to get your own unique options and environment variables from one of the following and change the processing according to their presence or absence:

Example of how the program handles Script Arguments

The last part of the story of creating a command line application in python is
cli.py, or
When creating command-line applications in Python,
of the model
Getting Command Line Arguments using argparse, etc.

Example of how to retrieve Environment Variables in a program

Difference between Python os.getenv and os.environ.get

os.getenv('ENV_VAL')
os.environ.get('ENV_VAL')
os.environ ['ENV_VAL']


2022-09-30 17:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.