VS Code version: version Version: 1.69.2 (Universal))
Extension version (available under the Extensions sidebar): v2021.12.1559732655
OS and version: MacOS 12.3.1
Python version: 3.6.8
Type of virtual environment used (N/A | venv | virtualenv | conda | …): venv
Relevant/affected Python packages and their versions: pytest==7.0.1
problem:
~/wkspace/one_service/.venv/bin/python ~/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ~/wkspace/one_service -s --cache-clear tests
cwd: ~/wkspace/one_service
[ERROR 2022-7-10 14:19:35.323]: Error discovering pytest tests:
r [Error]: LOADING_FILE: app.__init__.py
BASE_DIR in app.__init__.py: /Users/77/wkspace/one_service
SRE::START_ENV::[None]
b'xe5x8axa0xe8xbdxbdxe9x85x8dxe7xbdxaexe6x96x87xe4xbbxb6xe4xb8xbaxefxbcx9aenv_local.py'
ImportError while loading conftest '/Users/77/wkspace/one_service/tests/conftest.py'.
tests/conftest.py:3: in <module>
from app import app
app/__init__.py:165: in <module>
print(f"os.environu73afu5883u53d8u91cfuff1a{os.environ}")
E UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-14: ordinal not in range(128)
Traceback (most recent call last):
File "/Users/77/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/testing_tools/run_adapter.py", line 22, in <module>
main(tool, cmd, subargs, toolargs)
File "/Users/77/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/testing_tools/adapter/__main__.py", line 100, in main
parents, result = run(toolargs, **subargs)
File "/Users/77/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 44, in discover
raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 4)
at ChildProcess.<anonymous> (/Users/tiantian/.vscode/extensions/ms-python.python-2021.12.1559732655/out/client/extension.js:32:39076)
at Object.onceWrapper (node:events:510:26)
at ChildProcess.emit (node:events:390:28)
at ChildProcess.emit (node:domain:475:12)
at maybeClose (node:internal/child_process:1064:16)
at Socket.<anonymous> (node:internal/child_process:450:11)
at Socket.emit (node:events:390:28)
at Socket.emit (node:domain:475:12)
at Pipe.<anonymous> (node:net:687:12)
2
Answers
Since none of the above answers solved my problem, I recently google the information myself and found the root cause. I added the following statement to the conftest.py file
The result obtained is
Checked the documentation to confirm the environment variables that affect sys.stdout.encoding
because the test find script was found to execute successfully in terminal.
In the terminal, look at these two environment variables and add them to the .env file.
The problem is solved.
First, please check whether the file encoding format in the lower right corner of vscode is
utf-8
(if not, please modify it).Then, based on your error message, provide the following solutions:
Method 1: Add utf-8 encoding format at the beginning of the file
Method 2: Use the following command in the terminal to update the encoding format
Method 3: Find the wrong place in your code and clearly indicate how it is coded
If the above solutions do not resolve your issue, please show a more detailed error message and your error code.