英文:
Can't run sh on virtual env or macOS
问题
我无法在虚拟环境或 macOS 上运行 sh 脚本。
脚本如下:
#!/bin/bash
# This script sets up the environment for a Flask project.
echo "Starting script..."
# Initialize virtual environment.
echo "Activating virtual environment..."
source env/bin/activate
# Define environment variables.
echo "Setting environment variables..."
export FLASK_APP=app.py
export FLASK_ENV=development
echo $FLASK_APP
echo "Script completed."
结果只显示了 echo 路径,但 source 和 export 命令都不起作用。
(base) user@xxx % sh envset.sh
Starting script...
Activating virtual environment...
Setting environment variables...
application.py
Script completed.
(base) user@xxx %
虚拟环境未被激活。
英文:
I can't run the sh script on virtual env or macOS
#!/bin/bash
# This script sets up the environment for a Flask project.
echo "Starting script..."
# Initialize virtual environment.
echo "Activating virtual environment..."
source env/bin/activate
# Define environment variables.
echo "Setting environment variables..."
export FLASK_APP=app.py
export FLASK_ENV=development
echo $FLASK_APP
echo "Script completed."
The result only shows the echo path but neither the source or the export commands work.
(base) user@xxx % sh envset.sh
Starting script...
Activating virtual environment...
Setting environment variables...
application.py
Script completed.
(base) usser@xxx %
The env wasn't activated.
答案1
得分: 1
(base) user@xxx % sh envset.sh
你在这里调用一个shell,作为终端shell的子进程。子进程永远不会更改其父进程的环境。相反,你应该使用`.`(可移植的`source`)执行文件,它将在终端当前的shell进程中执行其命令。然后,在`source`完成后,exports将对终端shell可用:
(base) user@xxx % . envset.sh
英文:
> ```
> (base) user@xxx % sh envset.sh
you're invoking a shell here as a child process of the terminal's shell. a child process never changes its parent process's environment.
Instead you should .
(portable source
) the file which will execute its commands in the terminal's current shell process. Then, the exports will be available to the terminal shell after the source
completes:
(base) user@xxx % . envset.sh
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论