以下是一个使用Python编写的Linux环境搭建自动化脚本示例
#!/usr/bin/env python3
import os
import sys
import subprocess
def install_dependencies():
print("Installing dependencies...")
try:
subprocess.run(["sudo", "apt-get", "update"], check=True)
subprocess.run(["sudo", "apt-get", "install", "-y", "python3-pip", "build-essential", "libssl-dev", "libffi-dev", "python3-dev"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred while installing dependencies: {e}")
sys.exit(1)
def create_virtualenv():
print("Creating virtual environment...")
try:
subprocess.run(["python3", "-m", "venv", "myenv"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred while creating virtual environment: {e}")
sys.exit(1)
def activate_virtualenv():
print("Activating virtual environment...")
os.environ["VIRTUAL_ENV"] = "myenv"
os.environ["PATH"] = f"{os.path.join(os.environ['VIRTUAL_ENV'], 'bin')}:{os.environ['PATH']}"
def install_python_packages():
print("Installing Python packages...")
try:
subprocess.run(["pip", "install", "requests"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred while installing Python packages: {e}")
sys.exit(1)
def main():
install_dependencies()
create_virtualenv()
activate_virtualenv()
install_python_packages()
print("Environment setup completed successfully!")
if __name__ == "__main__":
main()
这个脚本首先安装了所需的依赖项,然后创建了一个Python虚拟环境,并在其中安装了requests
库。请注意,这个脚本可能需要根据您的具体需求进行修改。
要运行此脚本,请将其保存为setup_environment.py
,并确保它具有可执行权限(使用chmod +x setup_environment.py
)。然后,通过运行./setup_environment.py
来执行脚本。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。