您好,登錄后才能下訂單哦!
以下是一個使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。