使用 Xampp 运行 Python 脚本 [英] Running Python scripts with Xampp
问题描述
我使用的是 python 2.7.13
起初浏览器显示的是原始代码.
我做了什么:
编辑 httpd.conf
AddHandler cgi-script .cgi .pl .asp .py
在我所有脚本的顶部,我添加了以下内容:
#!j:/Installeds/Python/python打印内容类型:文本/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: 不知道如何产生子进程: C:/Files and Installs/Xampp/htdocs/Test/main.py
AH02102:C:/Files and Installs/Xampp/htdocs/Test/main.py 不可执行;确保解释的脚本有#!"或'!"第一行
在 XAMPP for Windows 中运行 Python
步骤 1:下载并安装 Python
从
复制以下代码并粘贴到文件末尾:
AddHandler cgi-script .pyScriptInterpreterSource Registry-Strict
步骤 2.5:将 Python 扩展添加到默认页面位置(可选)
在 httpd.conf
文件中搜索
以将 index.py
等添加到默认页面列表中位置.
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
在XAMPPhtdocs
目录下创建文件夹和Python文件;例如.../xampp/htdocs/PythonProject/test.py
.
在脚本的开头,您首先需要指定 Python 可执行文件的目录.Python 3.10.0 的默认位置是 C:/Users/
,但在你的情况下,它可能会有所不同,取决于您安装 Python 的版本和目录.
#!C:/Users//AppData/Local/Programs/Python/Python310/python.exe
之后,您就可以创建 Python 脚本了.
#!C:/Users//AppData/Local/Programs/Python/Python310/python.exe打印(内容类型:文本/html\n")打印(你好,世界!")
保存文件并在 Web 浏览器中打开 localhost/PythonProject/test.py
.您的 Python 脚本应该正在运行.
I'm using python 2.7.13
At first the browser was showing the raw code.
what I did:
Edited httpd.conf
AddHandler cgi-script .cgi .pl .asp .py
At the top of all my scripts I added this:
#!j:/Installeds/Python/python
print "Content-type: text/html\n\n"
Now it's giving me Internal Server Error (500)
and I have no idea what else to try... First time with python.
Obs: I think this may help> 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
Run Python in XAMPP for Windows
Step 1: Download and Install Python
Download and install the latest version of Python from https://www.python.org/downloads.
Step 2: Configure XAMPP for Python
Open the Apache httpd.conf
configuration file located at .../xampp/apache/conf/httpd.conf
using a text editor of your choice.
The XAMPP GUI can also quickly access the httpd.conf
file:
Copy and paste the following code at the end of the file:
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
Step 2.5: Add Python extension to default page locations (Optional)
Inside the httpd.conf
file search for <IfModule dir_module>
to add index.py
among others to the list of default page locations.
<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>
Step 3: Restart Apache / XAMPP
If Apache was running while editing, now is the time to restart it.
Step 4: Run Python from XAMPP
Create a folder and Python file in the XAMPP htdocs
directory; e.g. .../xampp/htdocs/PythonProject/test.py
.
At the beginning of your script, you first need to specify the directory of your Python executable. The default location of Python 3.10.0 is C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe
, but in your case, it may be different, depending on the version and directory in which you've installed Python.
#! C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe
After that, you can create your Python script.
#! C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe
print("Content-Type: text/html\n")
print("Hello, World!")
Save the file and open localhost/PythonProject/test.py
in your web browser. Your Python script should be running.
这篇关于使用 Xampp 运行 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!