gpt4 book ai didi

python - 使用 Xampp 运行 Python 脚本

转载 作者:太空狗 更新时间:2023-10-29 20:34:20 26 4
gpt4 key购买 nike

我正在使用 python 2.7.13
起初,浏览器显示的是原始代码。

我做了什么:

编辑httpd.conf

AddHandler cgi-script .cgi .pl .asp .py  

在我所有脚本的顶部,我添加了这个:

#!j:/Installeds/Python/python   
print "Content-type: text/html\n\n"

现在它给我 Internal Server Error (500) 我不知道还能尝试什么...第一次使用 python。

Obs:我认为这可能有帮助> Apache>Error.log

[cgi:error] [pid 6364:tid 1620] (9)Bad file descriptor: [client ::1:51083] AH01222: don't know how to spawn child process: C:/Files and Installs/Xampp/htdocs/Test/main.py
AH02102: C:/Files and Installs/Xampp/htdocs/Test/main.py is not executable; ensure interpreted scripts have "#!" or "'!" first line

最佳答案

在 Windows 版 XAMPP 中运行 Python

第 1 步:下载并安装 Python

https://www.python.org/downloads 下载并安装最新版本的 Python .

第 2 步:为 Python 配置 XAMPP

打开 Apache httpd.conf配置文件位于 .../xampp/apache/conf/httpd.conf使用您选择的文本编辑器。

XAMPP GUI 还可以快速访问 httpd.conf文件:

enter image description here

将以下代码复制并粘贴到文件末尾:

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

步骤 2.5:将 Python 扩展添加到默认页面位置(可选)

httpd.conf里面文件搜索 <IfModule dir_module>添加index.py以及默认页面位置列表。

<IfModule dir_module>
DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm index.py \
default.php default.pl default.cgi default.asp default.shtml default.html default.htm default.py \
home.php home.pl home.cgi home.asp home.shtml home.html home.htm home.py
</IfModule>

第 3 步:重启 Apache/XAMPP

如果 Apache 在编辑时正在运行,现在是时候重新启动它了。

第 4 步:从 XAMPP 运行 Python

在 XAMPP 中创建文件夹和 Python 文件 htdocs目录;例如.../xampp/htdocs/PythonProject/test.py .

在脚本的开头,您首先需要指定 Python 可执行文件的目录。 Python 3.10.0 的默认位置是 C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe ,但在您的情况下,它可能会有所不同,具体取决于您安装 Python 的版本和目录。

#! C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe

之后,您可以创建 Python 脚本。

#! C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe

print("Content-Type: text/html\n")
print("Hello, World!")

保存文件并打开localhost/PythonProject/test.py在您的网络浏览器中。您的 Python 脚本应该正在运行。

关于python - 使用 Xampp 运行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42704846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com