简体   繁体   English

"当我尝试使用 venv 激活虚拟环境时出现错误"

[英]When I try to activate a virtual environment with venv I get an error

I am following this tutorial on how to set up a virtual environment in Python3.6 using the windows command prompt.我正在关注本教程,了解如何使用 windows 命令提示符在 Python3.6 中设置虚拟环境。 I had no problem generating the necessary file by running this:通过运行以下命令生成必要的文件没有问题:

python3 -m venv venv-test

The tutorial then advises to activate the virtual environment by running this code:本教程然后建议通过运行以下代码来激活虚拟环境:

venv-test/Scripts/activate

But when I do this , I get the error...但是当我这样做时,我得到了错误......

 File "venv-test/Scripts/activate", line 4
    deactivate () {
              ^ SyntaxError: invalid syntax

I opened the generated 'activate' file and this seems to be the part that's causing the problem but I'm not sure how to correct it.我打开了生成的“激活”文件,这似乎是导致问题的部分,但我不确定如何纠正它。

deactivate () {
# reset old environment variables
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
    PATH="${_OLD_VIRTUAL_PATH:-}"
    export PATH
    unset _OLD_VIRTUAL_PATH
fi
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
    PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
    export PYTHONHOME
    unset _OLD_VIRTUAL_PYTHONHOME
fi

Thanks in advance!提前致谢!

Try this:试试这个:

source venv-test/bin/activate

It worked here.它在这里工作。 Make sure you're in the folder that contains your venv, otherwise just mention its full path.确保您位于包含您的 venv 的文件夹中,否则只需提及其完整路径。

I haven't got access to a windows machine at the moment but if I remember correctly you need to run activate.bat and I think you need the full path.我目前无法访问 Windows 机器,但如果我没记错的话,您需要运行 activate.bat 并且我认为您需要完整路径。

So something like:所以像:

C://code/project/venv-test/Scripts/activate.bat

not sure the reason why running不确定跑步的原因

venv-test/Scripts/activate

doesn't work but if you go into the directory venv-test/Scripts and THEN type activate it works fine.不起作用,但如果您进入目录 venv-test/Scripts 并输入 activate ,它可以正常工作。

在 Python 3.8 上,只需转到包含虚拟环境的文件夹并键入

source venv-test\scripts\activate

你必须运行 source venv-test/bin/activate 而不是 venv-test/Scripts/activate


I had the same problem before, try typing :我之前也遇到过同样的问题,试试输入:

venv-test/Scripts/activate.bat,

it worked for me它对我有用

change directory to scripts directory as follows:将目录更改为脚本目录,如下所示:

cd venv-test/Scripts

and run the activate file by typing并通过键入运行激活文件

activate 

at the prompt在提示下

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

相关问题 使用venv时是否需要激活虚拟环境 - Do I need to activate virtual environment when using venv 我在编辑器中创建了一个虚拟环境,但是当我想通过此代码激活它时(kivy_venv/Scripts/activate)编辑器给了我错误消息 - I created a virtual environment in my editor but when I wanted to activate it by this code (kivy_venv/Scripts/activate) editor gave me error message zsh:当我尝试在 venv 中运行应用程序时,中止 python 错误 - zsh: abort python error when I try to run the app in venv 为什么创建环境后无法激活venv? - Why can't I activate venv after creating an environment? 尝试激活虚拟环境时出现PYTHONPATH错误 - PYTHONPATH error when trying to activate a virtual environment 为什么在激活虚拟环境时会出现语法错误? - Why do I get a syntax error when activating a virtual environment? 我无法在 conda 中激活虚拟环境 - I can not activate virtual environment in conda 如何在Powershell 2中激活虚拟环境? - How do I activate a virtual environment in Powershell 2? 使用命令“virtualenv venv”创建Python虚拟环境时出错 - Error when using the command "virtualenv venv" to create Python virtual environment 使用venv虚拟环境时,我应该将哪些文件提交到我的git存储库? - When working with a venv virtual environment, which files should I be commiting to my git repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM