【问题标题】:Python fails to run from shell despite being installed and py command running尽管已安装并且 py 命令正在运行,但 Python 无法从 shell 运行
【发布时间】:2020-03-07 15:20:39
【问题描述】:

我正在尝试按照为 Django 设置应用程序的教程进行操作,但无法通过此部分;
“python manage.py runserver”来自https://docs.djangoproject.com/en/2.2/intro/tutorial01/

PS C:\Users\Elias\Documents\WebApp\mysite> ls


    Directory: C:\Users\Elias\Documents\WebApp\mysite


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2019-11-11   4:32 PM                mysite
-a----       2019-11-11   4:32 PM            647 manage.py


PS C:\Users\Elias\Documents\WebApp\mysite> py manage.py runserver
PS C:\Users\Elias\Documents\WebApp\mysite> python manage.py runserver
Program 'python' failed to run: No application is associated with the specified file for this operationAt line:1 char:1
+ python manage.py runserver
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~.
At line:1 char:1
+ python manage.py runserver
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
    + FullyQualifiedErrorId : NativeCommandFailed

PS C:\Users\Elias\Documents\WebApp\mysite> py -m manage.py runserver
PS C:\Users\Elias\Documents\WebApp\mysite> python ./manage.py runserver
Program 'python' failed to run: No application is associated with the specified file for this operationAt line:1 char:1
+ python ./manage.py runserver
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
At line:1 char:1
+ python ./manage.py runserver
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
    + FullyQualifiedErrorId : NativeCommandFailed

PS C:\Users\Elias\Documents\WebApp\mysite> ./manage.py runserver
PS C:\Users\Elias\Documents\WebApp\mysite> python -V
Program 'python' failed to run: No application is associated with the specified file for this operationAt line:1 char:1
+ python -V
+ ~~~~~~~~~.
At line:1 char:1
+ python -V
+ ~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
    + FullyQualifiedErrorId : NativeCommandFailed

PS C:\Users\Elias\Documents\WebApp\mysite> py -V
Python 3.8.0
PS C:\Users\Elias\Documents\WebApp\mysite> python -m django --version
Program 'python' failed to run: No application is associated with the specified file for this operationAt line:1 char:1
+ python -m django --version
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~.
At line:1 char:1
+ python -m django --version
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
    + FullyQualifiedErrorId : NativeCommandFailed

PS C:\Users\Elias\Documents\WebApp\mysite> py -m django --version
2.2.7
PS C:\Users\Elias\Documents\WebApp\mysite> py ./manage.py runserver
PS C:\Users\Elias\Documents\WebApp\mysite> py ./manage.py runserver 8080

如您所见,Django 已安装(但出于任何奇怪的原因,您只能看到使用 'py' 而不是 'python' 的版本)。即使如此,“py ./manage.py runserver”仍无法从端口 8000 或 8080 在 127.0.0.1 上运行服务器。所有环境变量均设置在
LOCALAPPDATA%\Programs\Python\Python38-32;\%LOCALAPPDATA%\Programs\Python\Python38-32\Lib\site-packages;%LOCALAPPDATA%\Programs\Python\Python38-32\Scripts。

【问题讨论】:

    标签: python django python-3.x


    【解决方案1】:

    在您的环境变量中,您必须只设置\Python38-32\ 的路径而不是站点包,原因是那里有一个python.exe 文件可以帮助您运行python manage.py runserver 命令。

    还要确保你安装了 Django,推荐的方法是使用virtualenv,但如果你是初学者也可以

    【讨论】:

    • 我确实这样做了,我从脚本和站点包中删除了环境变量,只指向 \Python38-32\。然而同样的问题仍然存在。我可以使用“py”,但不能使用“python”。
    【解决方案2】:

    我通过重新安装 python 并单击选项以禁用安装时的 MAX_PATH 字符限制来修复它。

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 1970-01-01
      • 2022-10-15
      • 2020-12-17
      • 2021-10-04
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 2018-07-30
      相关资源
      最近更新 更多