gpt4 book ai didi

python - 如何创 build 置脚本以使 python 脚本在 Ubuntu 环境中可用?

转载 作者:行者123 更新时间:2023-12-04 19:26:43 26 4
gpt4 key购买 nike

我是 Python 的新手。我已经完成了一些 python 脚本,我在 windows 环境中编写。我想让该脚本在 Ubuntu 上可用,而无需像在 Windows 环境中那样再次安装包。我期望当我将脚本复制到 Ubuntu 环境时,它会直接运行。可能吗?
到目前为止,我写了一个 setup.py 来检查安装,但是当我在 Ubuntu 环境下运行它时,它需要安装一些 pip再次打包。

import subprocess

req = {
"pip", "pyunpack", "patool", "Archive"
}


def existing_package():
result = subprocess.run(["pip", "list"], stdout=subprocess.PIPE)
result = result.stdout.decode('utf-8').split("\n")
return [r.split()[0] for r in result[2:-1]]


def main():
exist = set(existing_package())
if req.issubset(exist):
print(f"✓ All package has installed successfully\n")
else:
print(f"This package {req - exist} is not installed")
input("Press Enter to install...")
for request in req - exist:
subprocess.run(["pip", "install", request])


if __name__ == "__main__":
main()
任何人都可以给我想法。太感谢了

最佳答案

有很多方法可以确保脚本安装了所有必需的包。最简单的方法是使用pip,这里是一个例子

# While developing
pip install pyunpack patool Archive
pip freeze > requirements.txt
# On your (ubuntu) server
pip install -r requirements.txt
虽然这很可能会起作用,但不建议这样做,主要是因为您可能会更新 Ubuntu 本身使用的软件包。
以下是更优雅地实现这一点的方法列表,按复杂性排序。
  • virtual envs
  • pyenv
  • poetry
  • https://docs.docker.com/language/python/build-images/

  • (此列表远未完成,只是我过去喜欢使用的一些工具。)

    关于python - 如何创 build 置脚本以使 python 脚本在 Ubuntu 环境中可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70167806/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com