简体   繁体   English

如何在venv中运行Powershell脚本?

[英]How do you run a powershell script from within venv?

I'm trying to develop a SPA with PyCharm which uses venv as the virtual environment for my project. 我正在尝试使用PyCharm开发SPA,并将venv用作项目的虚拟环境。

I'm following this tutorial https://auth0.com/blog/using-python-flask-and-angular-to-build-modern-apps-part-1/ but the config is based on a linux system. 我正在按照本教程https://auth0.com/blog/using-python-flask-and-angular-to-build-modern-apps-part-1/进行操作,但配置基于linux系统。 I'm stuck on the part where I want to run a bootstrap script to run my flask app from the command line. 我被困在我想运行引导脚本以从命令行运行我的flask应用程序的部分。

It seemed reasonable to put this into a PowerShell script, as I'm developing on Windows 10, but I'm unable to run this script from within venv. 在Windows 10上进行开发时,将其放入PowerShell脚本中似乎很合理,但无法在venv中运行此脚本。

The script itself just contains the following: 该脚本本身仅包含以下内容:

$env:FLASK_APP = "./src/main.py" $ env:FLASK_APP =“ ./src/main.py”

flask run -h 0.0.0.0 烧瓶运行-h 0.0.0.0

Now, within venv I can only launch a PowerShell terminal. 现在,在venv中,我只能启动PowerShell终端。 I can't actually run this script from within venv directly. 我实际上不能直接在venv中运行此脚本。 If I navigate to the venv folder from within PowerShell my script will be executable but it's obviously not working as flask is only available within venv. 如果我从PowerShell中导航到venv文件夹,则我的脚本将是可执行的,但显然不起作用,因为flask仅在venv中可用。

So, please can somebody show me the best practice for running a script from within venv that will run my flask app? 因此,请有人告诉我从venv中运行将运行我的flask应用程序的脚本的最佳实践吗?

You can dot source the pipenv with : 您可以使用以下方法点播pipenv的源:

cd YourProjectPath
$venv = pipenv --venv
. "$venv\scripts\activate.ps1"
#"rest of your script"
$env:FLASK_APP = "./src/main.py"
flask run -h 0.0.0.0

if you are directly using venv , then: 如果您直接使用venv,那么:

. .\your env name\Scripts\Activate.ps1
#"rest of your script"
$env:FLASK_APP = "./src/main.py"
flask run -h 0.0.0.0

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

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