使用pipenv创建虚拟环境后,无法继续执行bash脚本



我正在编写一个bash脚本,该脚本自动化了创建django项目以及创建django项目所需的一些文件夹和文件的过程。

这里的问题是,当我使用pipenv shell命令创建和激活虚拟环境时,会创建并激活虚拟环境,但不会执行下一个命令。脚本只是等待退出虚拟环境,当我退出/停用虚拟环境时,bash脚本的下一行就会执行。

我的下一行是pipenv install django。当执行这一行时,django被安装在虚拟环境中,它再次等待用户退出虚拟环境,当我退出环境时,执行下一行。但是我的下一行是django-admin startproject myproject,当执行这一行时,我得到了错误command not found。是的,我知道我为什么会犯这个错误。环境已停用,无法识别django-admin命令。

此外,我正在以超级用户的身份运行此脚本。我用的是wsl2

我的代码:

mkdir "django_project"
cd "django_project"
pipenv shell
pipenv install django
read -p "Enter your project name: " PROJECT_NAME
django-admin startproject "$PROJECT_NAME"
cd "$PROJECT_NAME"
pipenv lock -r > requirements.txt

一旦我运行了这个脚本,我就会在命令行中得到它

Creating a Pipfile for this project...
Launching subshell in virtual environment...
root@LENOVO:/home/ubuntu/USER/Bash_Projects/test_folder/django_project#  . /root/.local/share/virtualenvs/django_project-IyKPiglU/bin/activate
(django_project) root@LENOVO:/home/ubuntu/USER/Bash_Projects/test_folder/django_project# exit
exit
Installing django...
Adding django to Pipfile's [packages]...
✔ Installation Succeeded 
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success! 
Updated Pipfile.lock (a6086c)!
Installing dependencies from Pipfile.lock (a6086c)...
🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
Enter your project name: test_project1
create_django_project.sh: line 6: django-admin: command not found
create_django_project.sh: line 7: cd: test_project1: No such file or directory

之后,我用^C终止了脚本

管道文件已成功创建。还将创建requirements.txt文件,并在requirements.txt文件中更新所有程序包。

我的问题是如何在激活环境后继续脚本而不将其停用?

我只使用pip3而不使用pipenv来修复此错误

pip3 install django

在超级用户模式下,使用此命令行

sudo -s

它对我有用我的机器是UBUNTU 20.04PYTHON 3.8.10版

最新更新