英文:
PyCharm runs a flask app but fails to debug it in python3.11
问题
我在我的计算机上有多个Python版本(3.8、3.9、3.10和3.11),分别用于不同的项目。所有版本都在PyCharm 2023.1.1上正常运行,除了3.11。
我有一个基于Flask的项目,使用了3.11,并且它也正常运行。然而,当我尝试调试它时,服务器启动然后抛出以下错误:
Connected to pydev debugger (build 231.8770.66)
*Serving Flask app 'app'
Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
Running on https://127.0.0.1:5001
Press CTRL+C to quit
Restarting with stat
C:\Users\SomeUser\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe: can't open file 'C:\\Program': [Errno 2] No such file or directory
Process finished with exit code 2
虚拟环境是由PyCharm解释器自动创建的,它正在使用python3.11。看起来python.exe试图打开一个不存在的名为Program的文件夹,我猜测这是Program Files,但我不明白为什么会出现这个问题。
我尝试过更改/添加PATH和PYTHONPATH,尝试过各种配置设置,安装/重新安装了python3.11和PyCharm,但到目前为止似乎没有解决问题。在我尝试旧版本的PyCharm之前,您有什么关于可能引发这个问题的建议吗?
我尝试更改了python3.11的环境变量。我尝试安装和重新安装了python3.11和PyCharm。我尝试更改了设置。我在Pycharm中启用了Python调试器的g-event兼容性。我没有尝试使用较旧的PyCharm版本。
英文:
I have multiple python versions on my machine (3.8, 3.9, 3.10 and 3.11) used with different projects. All versions run fine with PyCharm 2023.1.1 except 3.11.
I have a flask-based project which uses 3.11 and it runs fine. Nevertheless, when I try to debug it, the server starts and then throws the following error:
Connected to pydev debugger (build 231.8770.66)
*Serving Flask app 'app'
Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
Running on https://127.0.0.1:5001
Press CTRL+C to quit
Restarting with stat
C:\Users\SomeUser\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe: can't open file 'C:\\Program': [Errno 2] No such file or directory
Process finished with exit code 2
The virtual environment was created by the PyCharm interpreter automatically and it is using python3.11. It also seems that python.exe tries to open a nonexistent folder called Program which I assume is Program Files, but I do not get why.
I tried changing/adding PATHs and PYTHONPATHs. Played with various configuration settings. Installed-reinstalled both python3.11 and PyCharm and so far nothing seems to work.
Any suggestions on what might be causing the issue, before I try an old version of PyCharm?
I tried changing environment variables for python3.11. I tried installing and reinstalling both python3.11 and PyCharm. I tried changing the settings. I enabled the g-event compatibility for the Python Debugger in Pycharm. What I did not try is using an older PyCharm version.
答案1
得分: 1
在**....\Lib\site-packages\werkzeug_reloader.py**文件中,大约在第272行的def restart_with_reloader(self)
函数内,找到如下代码:
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
在这行代码之前插入以下代码,用双引号括起每个包含空格的参数:
args = [ f'""{a}""' if ' ' in a else a for a in args ]
这样你将得到:
def restart_with_reloader(self) -> int:
"""Spawn a new Python interpreter with the same arguments as the
current one, but running the reloader thread.
"""
while True:
_log("info", f" * Restarting with {self.name}")
args = _get_args_for_reloading()
new_environ = os.environ.copy()
new_environ["WERKZEUG_RUN_MAIN"] = "true"
## WORK-AROUND FIX ##
args = [ f'""{a}""' if ' ' in a else a for a in args ]
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
if exit_code != 3:
return exit_code
英文:
Here is a work-around that fixes the problem for me for Pycharm community edition running on Windows.
Edit the WerkZeug library file: ....\Lib\site-packages\werkzeug_reloader.py at around line 272 in function: def restart_with_reloader(self)
just before the call to:
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
Insert this line of code which surrounds every arg that contains a space with a pair of double-quotes:
args = [ f'""{a}""' if ' ' in a else a for a in args ]
So you get:
def restart_with_reloader(self) -> int:
"""Spawn a new Python interpreter with the same arguments as the
current one, but running the reloader thread.
"""
while True:
_log("info", f" * Restarting with {self.name}")
args = _get_args_for_reloading()
new_environ = os.environ.copy()
new_environ["WERKZEUG_RUN_MAIN"] = "true"
## WORK-AROUND FIX ##
args = [ f'""{a}""' if ' ' in a else a for a in args ]
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
if exit_code != 3:
return exit_code
答案2
得分: 0
I got a similar issue after updating my environment. I re-tried with old python 3.10.5 but updated python packages and updated PyCharm. In that case the debugging mode of flask works just fine.
However, when using python 3.11, the flask debugging mode does not work any more. I got the error
FileNotFoundError: [WinError 2] The system cannot find the file specified
The reasons seems to be a bug of python 3.11 regarding sys.executable:
https://github.com/python/cpython/issues/102496
=> Try to temporarily fix the path at the beginning of your main program.
For me following line of code did the trick:
sys.executable = sys.executable.replace('\\App', '\\..\\..\\App')
英文:
I got a similar issue after updating my environment. I re-tried with old python 3.10.5 but updated python packages and updated PyCharm. In that case the debugging mode of flask works just fine.
However, when using python 3.11, the flask debugging mode does not work any more. I got the error
FileNotFoundError: [WinError 2] The system cannot find the file specified
The reasons seems to be a bug of python 3.11 regarding sys.executable:
https://github.com/python/cpython/issues/102496
=> Try to temporarily fix the path at the beginning of your main program.
For me following line of code did the trick:
sys.executable = sys.executable.replace('\\App', '\\..\\..\\App')
答案3
得分: 0
我尝试将路径字符串中的所有空格删除以供PyCharm使用,这解决了问题。也就是说,如果我将其安装在一个自定义文件夹中,例如 C:/PyCharm
,并将PyCharm自动生成的文件夹 PyCharm 2023 重命名为 PyCharm_2023 ,它也能正常工作。
英文:
I tried removing all empty spaces in the path string to PyCharm and this fixes the issue. That is, if I install it in a custom folder for example C:/PyCharm
and rename the PyCharm autogenerated folder PyCharm 2023 to PyCharm_2023 it also works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论