简体   繁体   English

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

[英]I Can use python in cmd but not in python.exe?

I can use cmd to run python, But I cannot use python.exe directly.我可以使用 cmd 来运行 python,但我不能直接使用 python.exe。 I have already built python environment,but when I turn on a python file,the program just crashes.我已经搭建了 python 环境,但是当我打开一个 python 文件时,程序就崩溃了。

python version: python 版本:
3.10

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

the code showed below代码如下所示

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 is not very friendly for double-clicking on console programs. Windows 对于双击控制台程序不是很友好。 It will run the program in a temporary console, which will disappear after it finishes.它将在临时控制台中运行程序,完成后该控制台将消失。 You have tried to remedy this with your input statement at the end, which is a good thing to do.您已尝试使用最后的input语句来解决此问题,这是一件好事。 However, this only works if your program runs without errors.但是,这仅在您的程序运行没有错误时才有效。 When you get an exception, this statement will never execute, and the console will disappear before you get a chance to read the error messages.当您遇到异常时,该语句将永远不会执行,并且控制台将在您有机会阅读错误消息之前消失。

So I suggest to catch the possible exceptions and print them before the input statement.所以我建议在input语句之前捕获可能的异常并打印它们。 One possible exception that I could see, is when this script is run in a working directory where you have no write access.我可以看到的一个可能的例外是,当此脚本在您没有写入权限的工作目录中运行时。

Try this and see if there is an error message.试试这个,看看是否有错误信息。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM