在Windows中运行Python的IDLE

wtlkbnrh  于 2023-08-07  发布在  Windows
关注(0)|答案(3)|浏览(178)

我搞砸了我的空闲快捷方式。在Windows中从cmd.exe shell启动IDLE的方法是什么?

kr98yfug

kr98yfug1#

在我的系统上,运行C:\Python26\lib\idlelib\idle.py会从命令提示符启动idle。显然,如果Python主目录不是C:\Python26\,则需要调整路径。
看起来你也可以在同一个目录下通过idle.pywidle.bat启动它。

1l5u6lss

1l5u6lss2#

您可以在我的电脑属性-->高级中的环境变量选项卡中添加一个路径作为c:\Python27\Lib\idlelib。添加此路径后,只要在cmd中写入idle.pyw,即可运行IDLE。
只要确保您将文件夹名称替换为您拥有的任何目录名称即可。

pkmbmrz7

pkmbmrz73#

Step 1: Create the Python Script (myscript.py) Open a text editor and write the Python script as follows:

import os
import random
import pandas as pd 

# Create the directory if it does not exist
if not os.path.exists(f'C:\\Users\\%USERNAME%\\Desktop'):
    os.makedirs(f'C:\\Users\\%USERNAME%\\Desktop')

# Generate new random data
names = ["John", "Jane", "Michael", "Emma", "William"]
ages = [random.randint(20, 50) for _ in range(len(names))]
states = [random.choice(["CA", "NY", "TX"]) for _ in range(len(names))]
salaries = [random.randint(25000, 80000) for _ in range(len(names))]

# Create the new Emp dictionary with random data
Emp = {"Name": names, "Age": ages, "State": states, "Salary": salaries}

DF = pd.DataFrame(Emp, index=[f"E{i}" for i in range(1, len(names)+1)])

# Save the data to a CSV file
DF.to_csv(f'C:\\Users\\%USERNAME%\\Desktop\\file.csv', index=False)
Save this Python script as myscript.py.

Step 2: Create the Batch File (myscript.bat) Open a text editor and write the following lines:

@cd %DIR%

@python.exe myscript.py %* 

@pause

Save this text as myscript.bat in the same folder where you have the Python script (myscript.py).

Now, double click the bat file, it should work.

If you encounter issues running the Python script from the batch file, you may need to add the Python executable path to the "system's" PATH variable. Follow these steps to do so:

Step 3: Add Python to the PATH variable

Locate the path to your Python installation by using following code:

import sys
print(sys.exec_prefix)

For example, if you are using an Anaconda environment and your environment name is "myenv_name," the path could be something like C:\Users\%USERNAME%\miniconda3\envs\myenv_name.

Open the Start menu, type "Environment Variables," and click on "Edit the system environment variables."

In the "System Properties" window, click on the "Environment Variables" button.

Under the "System variables" section, find the "Path" variable, and click on "Edit."

In the "Edit environment variable" window, click on "New" and add the path to your Python executable (e.g., C:\Users\%USERNAME%\miniconda3\envs\myenv_name).

Click "OK" to close all the windows.

Step 4: Run the Batch File Navigate to the folder where you have created both files (myscript.py and myscript.bat). 
Double-click on the myscript.bat file to execute your Python script.

The myscript.py will generate new random data for the "Name", "Age", "State", and "Salary" fields each time you run the script. The new data will then be saved to the CSV file at C:\\Users\\%USERNAME%\\Desktop\\file.csv.

By following these steps and adding Python to the PATH variable, you should be able to run your Python script successfully on Windows 11 using the batch file and generate different random data each time you execute the script.

字符串

相关问题