簡體   English   中英

在運行可執行的 python 腳本(例如 .command 文件)時,如何使用外部 python 模塊(例如模塊“請求”)?

[英]How to use external python modules such as the module 'requests' when running executable python scripts such as .command files?

我通過將 Python 腳本轉換為 .command 文件並在文件的第一行添加 #!/usr/bin/env python3 將其制作為可執行文件。

當我雙擊運行該文件時,該文件確實在終端上運行,但是安裝的模塊似乎沒有鏈接到該文件。

導入請求 ModuleNotFoundError: 沒有名為“請求”的模塊

在運行 python 腳本的可執行形式時,有沒有辦法使用外部模塊,例如“請求”模塊?

經過 ”。” 你的意思是 ”。/”?

你如何正常運行它?

假設使用 Linux,但可能也適用於 Mac:
首先定位python3在哪里:

which python3

對我來說這是/usr/bin/python3
然后在腳本的第一行添加:

#!/usr/bin/python3
import requests
print("something")

給文件添加執行權限:

chmod +x file.command

執行文件:

./file.command

如果您仍然收到ModuleNotFoundError ,請確保使用pip3 install requests為當前版本的 python 安裝了該庫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM