作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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 本身使用的软件包。
关于python - 如何创 build 置脚本以使 python 脚本在 Ubuntu 环境中可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70167806/
我是一名优秀的程序员,十分优秀!