Почему не активируется виртуальное окружение python
Перейти к содержимому

Почему не активируется виртуальное окружение python

  • автор:

Почему я не могу активировать виртульное окружение python?

Пытаюсь установить виртуальное окружение на py
Создаю виртуальное окружение
пишу python -m venv env и все ок, но активировать не получается. Пишу на винде, python 3.9.7 . Вот скрин консоли
6241f0c4abefd471558648.png
Как только уже не пытался ее активировать, не получается.

  • Вопрос задан более года назад
  • 4220 просмотров

Простой 1 комментарий

  • Facebook
  • Вконтакте
  • Twitter

phaggi

1. Открой PowerShell от имени администратора.
2. Введи команду «Set-ExecutionPolicy unrestricted» без кавычек.
3. Теперь можешь можешь активировать виртуальное окружение (не важно в этом же окне или в новом).

Эти действия нужно проделать только 1 раз, далее для запуска виртуального окружения их повторять не нужно.

  • Facebook
  • Вконтакте
  • Twitter

yxtiblin

попробовал, следующая ошибка 6241fc000669a931919870.png

не получается активировать виртуальную среду venv. (не появляется префикс)

PS C:\Users\user\PycharmProjects\pythonProject2> Set-ExecutionPolicy -Scope CurrentUser Bypass
PS C:\Users\user\PycharmProjects\pythonProject2> Get-ExecutionPolicy
Bypass
PS C:\Users\user\PycharmProjects\pythonProject2> venv\Scripts\Activate.ps1
(venv) PS C:\Users\user\PycharmProjects\pythonProject2> deactivate
PS C:\Users\user\PycharmProjects\pythonProject2> venv\Scripts\Activate.ps1
(venv) PS C:\Users\user\PycharmProjects\pythonProject2>

а можно подробней про это:
/Чтобы работать в окружении через PyCharm, надо указать окружение в настройках запуска программ /

Почему я не могу активировать виртуальное окружение в PyCharm

Подскажите, пожалуйста, столкнулся с проблемой, что при создании виртуального окружения в Pycharm, если открываю терминал через Windows PowerShell выдает ошибку:

Если открываю терминал Bash, то также нет сокращения venv в начале строки. На команду "pip freeze" отражает пустую строку, на команду

Не могу понять причину, т.к. папка venv создана в директории, путь указан на python.exe в этой папке. В чем может быть причина?введите сюда описание изображения

Ответы (2 шт):

Как у вас и сказано, у вас в системе отключено выполнение сценариев. Так как у вас Windows, то сценарии выполняются с помощью PowerShell. Соответственно, вам нужно включить выполнение сценариев:

Issue with virtualenv — cannot activate

I created a virtualenv around my project, but when I try to activate it I cannot. It might just be syntax or folder location, but I am stumped right now.

You can see below, I create the virtualenv and call it venv. Everything looks good, then I try to activate it by running source venv/bin/activate

I’m thinking it might just have to do with my system path, but not sure what to point it to (I do know how to edit the path). I’m on python 7 / windows os, virtual env 2.2.x

mrj's user avatar

33 Answers 33

source is a shell command designed for users running on Linux (or any Posix, but whatever, not Windows).

On Windows, virtualenv creates a .bat/.ps1 file, so you should run venv\Scripts\activate instead (per the virtualenv documentation on the activate script).

Just run activate , without an extension, so the right file will get used regardless of whether you’re using cmd.exe or PowerShell.

I had the same problem. I was using Python 2, Windows 10 and Git Bash. Turns out in Git Bash you need to use:

I was also facing the same issue in my Windows 10 machine. What steps i tried were:

Go to andconda terminal Step 1

You can check it via spider tool in anaconda by typing import tensorflow as tf

Tushar Srivastava's user avatar

For activation you can go to the venv your virtualenv directory by cd venv .

Then on Windows, type dir (on unix, type ls ). You will get 5 folders include , Lib , Scripts , tcl and 60

Now type .\Scripts\activate to activate your virtualenv venv .

Your prompt will change to indicate that you are now operating within the virtual environment. It will look something like this (venv)user@host:

And your venv is activated now.

Gautam Kumar's user avatar

source command is officially for Unix operating systems family and you can’t use it on windows basically. instead, you can use venv\Scripts\activate command to activate your virtual environment.

Made a Screenshot of how venv\Scripts\activate command works correctly

Mohammad Heydari's user avatar

Ensure venv is there and just follow the commands below. It works in Windows 10.

Go to the path where you want your virtual enviroments to reside:

Create the virtual environment named «env»:

Add the path to the git ignore file (optional):

Activate the virtual env:

Tried several different commands until I came across:

This did it for me. Setup: Win 10, python 3.7, gitbash. Gitbash might be the culprit for not playing nice with other activate commands.

For windows, type «C:\Users\Sid\venv\FirstProject\Scripts\activate» in the terminal without quotes. Simply give the location of your Scripts folder in your project. So, the command will be location_of_the_Scripts_Folder\activate.enter image description here

Siddharth Kumar Shukla's user avatar

if you already cd your project type only in windows 10

That works for me:)

A small reminder, but I had my slashes the wrong way on Win10 cmd. According to python documentation the activate command is: C:\> <venv>\Scripts\activate.bat When you’re browsing directories it’s e.g. cd .env/Scripts

So to create my venv I used python -m venv —copies .env and to activate .env\Scripts\activate.bat

  1. Open your powershell as admin
  2. Enter "Set-ExecutionPolicy RemoteSigned -Force
  3. Run "gpedit.msc" and go to >Administrative Templates>Windows Components>Windows Powershell
  4. Look for "Activate scripts execution" and set it on "Activated"
  5. Set execution directive to "Allow All"
  6. Apply
  7. Refresh your env

I have a hell of a time using virtualenv on windows with git bash, I usually end up specifying the python binary explicitly.

If my environment is in say .env I’ll call python via ./.env/Scripts/python.exe … , or in a shebang line #!./.env/Scripts/python.exe ;

Both assuming your working directory contains your virtualenv ( .env ).

You can run the source command on cygwin terminal

For windows Microsoft Tech Support it might be a problem with Execution Policy Settings. To fix it, you should try executing Set-ExecutionPolicy Unrestricted -Scope Process

Bharat Agrawal's user avatar

Instead of using:

Your Script will be activated.

ShraOne Devade's user avatar

If you’re using Windows, use the command «venv\Scripts\activate» (without the word source) to activate the virtual environment. If you’re using PowerShell, you might need to capitalize Activate.

open the folder with any gitbash console. for example using visualCode and Gitbash console program: 1)Install Gitbash for windows

2) using VisualCode IDE, right click over the project open in terminal console option

3) on window console in Visualcode, looking for a Select->default shell and change it for Gitbash

4)now your project is open with bash console and right path, put source ./Scripts/activate

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *