简体   繁体   English

从 linux bash 脚本激活 python 虚拟环境

[英]Activcate a python virtual environment from a linux bash script

I am writing a bash script in linux that creates and activates a Python venv and then installs from a requirements.txt.我正在 linux 中编写 bash 脚本,该脚本创建并激活 Python venv,然后从 requirements.txt 安装。 Like this像这样

python3 -m venv ~/myvenv/env
source ~/myvenv/env/bin/activate
cp requirements.txt ~/myvenv/env/requirements.txt
pip3 install -r ~/myvenv/env/requirements.txt

This doesn't work for me.这对我不起作用。 It seems to create the myvenv directory but then doesnt switch in and run the requirements.txt file.它似乎创建了 myvenv 目录,但随后没有切换并运行 requirements.txt 文件。

Is there a different way to activate it with source from within a bash script?是否有其他方法可以使用 bash 脚本中的source来激活它?

When you run script your shell spawns new process, activates it and then dies.当您运行脚本时,您的 shell 会产生新进程,激活它然后死亡。 That's why when you get back into your shell you see unactivated one.这就是为什么当您回到您的 shell 时,您会看到未激活的一个。

you can run your script just using source command.您可以仅使用 source 命令运行您的脚本。 source command will load it into your active shell. source 命令会将其加载到您的活动 shell 中。

source script.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM