Python项目PowerShell脚本

AI   2024-02-17 18:04   158   0  

创建虚拟环境、安装依赖

使用git拉完项目,为了和其他项目环境隔离,一般我们会创建一个虚拟环境,创建完成虚拟环境后激活虚拟环境、安装项目的依赖

下面是一个简单的脚本 install.ps1 ,使用Python3.10创建一个名为venv的虚拟环境:

# PowerShell script equivalent to the batch commands

# Stop the script on any error
$ErrorActionPreference = "Stop"

# Navigate to the virtual environment directory and activate it
# Assuming the script is run from the directory where 'venv' is located
try {
	conda create --prefix venv python=3.10
	conda activate .\venv
	pip install -r requirements.txt
} catch {
    Write-Error "Failed to activate the virtual environment. Please check if the virtual environment exists or if there are permission issues."
    exit
}


# Pause at the end
Read-Host -Prompt "Press Enter to continue..."


把这个脚本放到项目的根目录,和 requirements.txt 同级,如果不同级,就要修改路径。

如下面的运行脚本:

# PowerShell script equivalent to the batch commands

# Stop the script on any error
$ErrorActionPreference = "Stop"

# Navigate to the virtual environment directory and activate it
# Assuming the script is run from the directory where 'venv' is located
try {
    conda activate .\Langchain-Chatchat\venv
} catch {
    Write-Error "Failed to activate the virtual environment. Please check if the virtual environment exists or if there are permission issues."
    exit
}

# Run the Python script
try {
	cd Langchain-Chatchat
	# python init_database.py --recreate-vs
    python startup.py -a
} catch {
    Write-Error "An error occurred while running the Python script. Please check the script and try again."
    exit
}

# Pause at the end
Read-Host -Prompt "Press Enter to continue..."

不同级的有个问题,就是这段代码

cd Langchain-Chatchat
	# python init_database.py --recreate-vs
    python startup.py -a

换成

python .\Langchain-Chatchat\startup.py -a

执行会有问题,所以写成上面的。不知道为啥。

博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。
QQ
微信
打赏
扫一扫