我可以在 cmd 中使用 python 但不能在 python.exe 中使用?

I Can use python in cmd but not in python.exe?

我可以用cmd来运行 python,但是我不能直接用python.exe。 我已经构建了 python 环境,但是当我打开一个 python 文件时,程序就崩溃了。

python版本:
3.10

路径:
C:\users\user\appdata\local\programs\python\pyhon310\python.exe

代码如下

import requests
import json
import pandas as pd


url = requests.get(r'https://shopee.tw/api/v4/search/search_items?by=pop&limit=100&match_id=1778234&newest=0&order=desc&page_type=shop')
data = json.loads(url.text)
items_count = int(data['total_count'])

output = list()

for page in range(0, items_count, 100):


    url = requests.get(f'https://shopee.tw/api/v4/search/search_items?by=pop&limit=100&match_id=1778234&newest={page}&order=desc&page_type=shop')

    data = json.loads(url.text)

    for item in data['items']:
        
        item_name = item['item_basic']['name']

        
        item_price = str(item['item_basic']['price'])[:-5]
        
        item_historical_sold = item['item_basic']['historical_sold']
        
        item_view_count = item['item_basic']['view_count']
        
        print(f'{item_name}:{item_price}:{item_historical_sold}:{item_view_count}')
        # 把資料寫入列表
        output.append([item_name, item_price, item_historical_sold, item_view_count])
df = pd.DataFrame(output, columns=['商品名稱', '價格', '已售出', '瀏覽數'])
df.to_excel('列表JSON版12041301.xlsx', index=False)

input('Enter the any press to exit')

Windows对于双击控制台程序不是很友好。它将 运行 临时控制台中的程序,它会在完成后消失。您已尝试通过最后的 input 语句来解决此问题,这是一件好事。但是,这仅在您的程序 运行 没有错误时才有效。当您遇到异常时,此语句将永远不会执行,并且控制台会在您有机会阅读错误消息之前消失。

所以我建议捕获可能的异常并在input语句之前打印它们。我看到的一个可能的例外是,当此脚本 运行 在您没有写入权限的工作目录中时。

试试这个,看看是否有错误信息。

import requests
import json
import pandas as pd
import traceback

def main():
    # Your original code, intended 4 spaces
    url = requests.get(r'https://shopee.tw/api/v4/search/search_items?by=pop&limit=100&match_id=1778234&newest=0&order=desc&page_type=shop')
.... etc....
    df.to_excel('列表JSON版12041301.xlsx', index=False)

if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        traceback.print_exc()
    input('Press ENTER key to exit')