尝试在适用于 Windows 10 的 Ubuntu 20.04 WSL上使用pyenv安装新Python
版本失败,并显示以下输出:
username@hd1pcms0347:~$ pyenv install 3.9.4
Downloading Python-3.9.4.tar.xz...
-> https://www.python.org/ftp/python/3.9.4/Python-3.9.4.tar.xz
Installing Python-3.9.4...
python-build: use readline from homebrew
BUILD FAILED (Ubuntu 20.04 using python-build 20180424)
Inspect or clean up the working tree at /tmp/python-build.20210602162502.2268
Results logged to /tmp/python-build.20210602162502.2268.log
Last 10 log lines:
File "/tmp/python-build.20210602162502.2268/Python-3.9.4/Lib/ensurepip/__init__.py", line 210, in _main
return _bootstrap(
File "/tmp/python-build.20210602162502.2268/Python-3.9.4/Lib/ensurepip/__init__.py", line 129, in _bootstrap
return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
File "/tmp/python-build.20210602162502.2268/Python-3.9.4/Lib/ensurepip/__init__.py", line 38, in _run_pip …
Run Code Online (Sandbox Code Playgroud) 这是一个类似的情况我已经遇到过了几个月前使用pylint的事先pylance:
我的python 3.9x
- 脚本(使用VS Code
on Ubuntu 20.04 LTS
)从以下自定义“工具”导入开始:
import sys
sys.path.append(
'/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/'
)
import General.Misc.general_tools as tools
Run Code Online (Sandbox Code Playgroud)
现在,Pylance
声明:
Import "General.Misc.general_tools" could not be resolvedPylance (reportMissingImports)
Run Code Online (Sandbox Code Playgroud)
即使在程序执行期间模块被完美地导入,也会发生这种情况。
因此,为了确保Pylance
理解这是一个现有的模块路径,除了sys.path.append(..)
- 方法之外,我在settings.json
- 文件中添加了以下内容:
{
...
// Possible values: "Jedi", "Pylance", "Microsoft", "None".
"python.languageServer": "Pylance",
// NOTE on changing from microsoft to pylance language server: python.autoComplete.extraPaths --> python.analysis.extraPaths
// Docs: https://github.com/microsoft/pylance-release/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings
"python.analysis.extraPaths": [
"/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts"
],
... …
Run Code Online (Sandbox Code Playgroud) python-import python-3.x visual-studio-code vscode-settings pylance
当Pylance被介绍时,我提出了一个关于如何通常自定义 Pylance linting 的问题。在这里,人们可以找到几种自定义 Pylance 的方法,但没有介绍如何抑制、静音或实际禁用某些警告和错误。
回顾一下,使用pylint可以指定以下内容VS Code settings.json
来禁用特定错误/警告:
"python.linting.pylintArgs": [
"--disable=C0111"
]
Run Code Online (Sandbox Code Playgroud)
至于背景,由于过多的 Pylance(reportMissingImports) linting 错误尚未解决,并且由于项目需求,我与 Pylance 同时启用了 pylint。尽管如此,这些无数的 Pylance(reportMissingImports) linting 错误仍然很烦人,我想完全消除它们。
我想像Python
往常一样通过按VS-Code
下来调试我的本地代码:Windows 10
F5
我一年多前就开始出现这个错误,但最近它变得持续存在。
整个错误回溯:
$ /usr/bin/env 'DEBUGPY_LOG_DIR=c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891' c:\\Users\\username\\Projects\\project-venv\\Scripts\\python.exe c:\\Users\\username\\.vscode\\extensions\\ms-python.python-2021.8.1105858891\\pythonFiles\\lib\\python\\debugpy\\launcher 56721 -- c:\\Users\\username\\Projects\\project\\test_files\\prediction_performance_monitoring\\modified_app_for_docker_testing.py
Traceback (most recent call last):
File "C:\Users\username\.pyenv\pyenv-win\versions\3.8.9\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\username\.pyenv\pyenv-win\versions\3.8.9\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy\launcher\__main__.py", line 97, in
<module>
main()
File "c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy\launcher\__main__.py", line 53, in
main
launcher.connect(host, port)
File "c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy\launcher/../..\debugpy\launcher\__init__.py", line 34, in connect
sock.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Run Code Online (Sandbox Code Playgroud)
.vscode/launch.json …
python-3.x connection-refused visual-studio-code vscode-debugger
我尝试在现有字典的第二层实例化一个空字典,然后为其分配一个键值对,但 MyPy 抛出错误。
这是一个最小的示例,当激活 MyPy 检查时它将重现它:
result = {"Test": "something"}
result['key'] = {}
result['key']['sub_key'] = ["some string", "another string"]
Run Code Online (Sandbox Code Playgroud)
这里的错误将类似于:
mypy(error): Incompatible types in assignment (expression has type
"Dict[<nothing>, <nothing>]", target has type "List[str]")
Run Code Online (Sandbox Code Playgroud)
我该如何防止这个错误?根据类似的问题,建议这样做
result['key'] = {} # type: ignore
Run Code Online (Sandbox Code Playgroud)
作为一种解决方法,但这似乎不是很优雅,这就是为什么我想知道是否还有更多的事情可以做。
我已经将我自己编写的 Python 脚本组织在一个由多个子目录组成的树中,从已经包含在"python.autoComplete.extraPaths"
settings-json 中的父目录“Scripts”开始:
"python.autoComplete.extraPaths": ["/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts",
"/home/andylu/anaconda3/lib/python3.7/site-packages"]
Run Code Online (Sandbox Code Playgroud)
除此之外,我还包含了一个 Python 环境文件:
"python.envFile": "/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Visual_studio_code/vscode_own_scripts.env"
Run Code Online (Sandbox Code Playgroud)
其中包含该行
export PYTHONPATH=/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts:/home/andylu/anaconda3/lib/python3.7/site-packages
Run Code Online (Sandbox Code Playgroud)
所有这些以前都很好,我所有的脚本都分布在 1 个以上的单个目录级别,如下所示:
+---Scripts
| +---General
| | +---script_one.py
| | +---script_two.py
Run Code Online (Sandbox Code Playgroud)
当我在任何 python 脚本中导入时,例如script_one.py
,我用
import sys
sys.path.append(
"/home/andylu/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts/"
)
import General.script_one as one
Run Code Online (Sandbox Code Playgroud)
并且 pylint 正确识别了这个导入的脚本,而不会抛出上述VS Code pylint(import-error)
.
现在,情况不同了。脚本变得如此之多,我将子文件夹拆分General
为包含一个额外的子目录级别,以便更清晰地组织脚本:
+---Scripts
| +---General
| | +---Plotting
| | | +---script_one.py
| | | +---script_two.py
| | +---Misc
| …
Run Code Online (Sandbox Code Playgroud) 从相关讨论中我得到了以下答案:
另外,在代码中需要仔细检查的是主入口点是否受到
__name__=='__main__'
检查保护:Run Code Online (Sandbox Code Playgroud)if __name__ == '__main__': main()
根据结构,
multiprocessing.freeze_support()
如果您需要多处理支持,您可能还需要:IE:
Run Code Online (Sandbox Code Playgroud)if __name__ == '__main__': multiprocessing.freeze_support() main()
(如果您没有该结构,它可以解释为什么多处理多次执行您的主入口点代码)。
然而,我既没有实现上述第一个选项,也没有实现第二个选项,而且,我执行的任何脚本都可能发生这种突然的多线程行为。似乎旧的/以前的调试会话并没有在幕后真正终止,因为在一个会话中没有多线程,然后在调试时启动多会话。正常执行代码时也会发生这种情况,代码退出时出现错误,然后我(再次)进入调试会话。
我不知道如何实现它,但我应该把
if __name__ == '__main__':
multiprocessing.freeze_support()
main()
Run Code Online (Sandbox Code Playgroud)
在我作为主脚本执行的所有 python 脚本的开头,即我的入口点或顶级脚本?
从所附的屏幕截图中可以看出,在通过按下执行标准调试过程(之前定义了一些断点)时,偶尔会发生以下情况:F5
VS Code
这会导致大量额外的计算时间、滞后和冗余的控制台输出,因为相同的代码连续执行多次。
奇怪的是,这种行为似乎是随机发生的。只有有时我才能得出结论,我之前尝试启动调试会话,该会话在代码中的某个时刻被 中断,但对我来说,为什么要重新启动所有这些以前失败的调试会话以及新的调试会话,Uncaught Exception
这似乎不合逻辑VS Code
按下时为一F5
。
Shift+F5
当通过或 红色方形按钮中断调试程序时,有时会在 - 窗口右下角出现Stop
该消息;特别是当问题发生时,显示.timeout after 1000 ms
VS Code
call stack
launch.json
定义集成终端调试的my - 文件的相关部分是:
{
// !!THIS CONFIG-FILE …
Run Code Online (Sandbox Code Playgroud) python debugging multithreading visual-studio-code vscode-debugger
在Windows 10 上的WSL(Wsman Shell 命令行,版本 0.2.1)中,我安装了pyenv并尝试使用pyenv-virtualenvwrapper。然而,我似乎无法启动并运行它,即使使用与我的 Ubuntu PC 相同的配置文件.bashr
也是如此。.profile
接下来,我将描述我所做的尝试。
username@hd1pcms0347:~$ pyenv
pyenv 1.2.26
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
--version Display the version of pyenv
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
exec Run an executable with the selected Python version
global Set or show the global Python version(s)
help Display help for a command
hooks List …
Run Code Online (Sandbox Code Playgroud) python virtualenvwrapper pyenv windows-subsystem-for-linux pyenv-virtualenv
由于预提交挂钩甚至不允许bandit发出警告和提交,因此我需要找到一种方法从 python 脚本执行 bash 命令,而不会有 bandit 抱怨。
使用subprocess python 包,到目前为止,无论我做了什么,强盗总是抱怨。我使用了“.run()”、“.check_call()”、“.Popen()”等,但都没有使用,shell=True
但没有任何效果。
如果有一个安全的子流程替代方案,我也会感兴趣,但我确信它也必须以某种方式与子流程一起工作。
强盗不接受的示例:
import shlex
import subprocess
...
bash_command = (
f'aws s3 cp {source_dir} s3://{target_bucket_name} --recursive'
f' --profile {profile_name}')
subprocess.check_call(shlex.split(bash_command), text=True)
Run Code Online (Sandbox Code Playgroud) 直到大约 3 周前,我一直在使用pylintpython
来检查VS Code
.
然后,我启用了pylance
替换pylint
. 然而,pylance
并未在提供的特定 linter-listVS Code
中列出。
现在,pylance
不显示未使用的模块导入。我怀疑这不包含在 的默认 linting 参数中pylance
,因此我尝试找出如何修改它们,类似于使用 执行此操作的过程pylint
,例如此处记录并像这样实现(插入到settings.json
当前工作区的 - 文件中) ):
"python.linting.pylintArgs": [
"--max-line-length=80",
"--disable=W0142,W0403,W0613,W0232,R0903,R0913,C0103,R0914,C0304,F0401,W0402,E1101,W0614,C0111,C0301"
]
Run Code Online (Sandbox Code Playgroud)
我如何以类似的方式自定义 的 linting 规则pylance
?
我想在 PowerShell CLI 中以及批处理脚本中实现我可以做的事情:
PS C:\Users\andreas.luckert> $timestamp = Get-Date -UFormat "%d-%m-%Y--%R-UTC%Z" | ForEach-Object { $_ -replace ":", "." }
PS C:\Users\andreas.luckert> echo $timestamp
26-11-2021--15.55-UTC+01
Run Code Online (Sandbox Code Playgroud)
现在,在我的批处理脚本中,我尝试了类似于以下的方法
SET _timestamp=('Get-Date -UFormat "%d-%m-%Y--%R-UTC%Z" | ForEach-Object { $_ -replace ":", "." }')
Run Code Online (Sandbox Code Playgroud)
然而,它不起作用。
像这样的解决方案对我来说看起来有点老套,批处理变量的一般说明在这种情况下没有帮助,并且与我在一开始提到的漂亮干净的 PowerShell 命令相比,所有这些方法在语法方面都非常丑陋。此外,它们都不包括时区,这对我来说很重要。
powershell timestamp batch-file batch-processing timestamp-with-timezone
python ×7
pylance ×3
pylint ×3
python-3.x ×3
pyenv ×2
windows-subsystem-for-linux ×2
bash ×1
batch-file ×1
debugging ×1
homebrew ×1
importerror ×1
mypy ×1
powershell ×1
pylintrc ×1
subprocess ×1
timestamp ×1
type-hinting ×1
ubuntu ×1