Activating Python Virtual Environment on Windows 11 - python

I am trying to create a venv virtual enviroment for Python in Window's command prompt. I created the enviroment; however, I am having difficulties using it because when I run the "activate" command it is not working. I think the problem is related to that the virtual enviroment does not have a scripts file like other window machines do, but rahter a bin file which has the activate script. When I run the activate command with the bin in the file directory I still get an error.
enter image description here
I have been trying to solve the problem for the past 4-5 hours and am completely stuck. I tried destroying and reconstructing the virtual enviroment, I tried using different extensions (.bat, .exe, .ps1, and just \activate), and tried using powershell.
Please let me know if you have any ideas what I am doing wrong!

Open a command prompt terminal by either searching command prompt in the Windows search bar, or press the Windows Key + R and enter cmd.
Create the virtual environment in a desired directory using the following command:
python -m venv env
This will create a new folder called env inside the directory where you executed the command.
You can activate the created virtual environment by running the following command in the same directory where you executed the last command:
cd env/Scripts && activate && cd ../../
I hope this helps.

python -m venv venv
#To run this command prompt on python terminal to create a virtual environment
venv\Scripts\activate
#Run this command prompt on python terminal

Related

Python django cant activate virtual enviroment

I can't activate virtual enviroment and get 'cannot be loaded because running scripts is disabled on this system'
I tried to write 'activate' and './activate' but both dont work
To solve this error:
Simply you can open powershell as admin and then execute below command:
set-executionpolicy remotesigned
You will be prompted to accept the change, type A(Yes to all), and press ENTER on your keyboard to allow the change.
Close the PowerShell admin window, and go back to the PowerShell Window where you got the error. Run the command below
venv\Scripts\activate
And now your error is solved.
If you are using virtualenv.
venv bin/activate
I am condering venv is your envionment folder and you're on the folder containing your virtual environment.
PS: you can create virtual environment using following command
virtualenv venv -p python3

how to visualize the python venv name in MacOs zsh prompt

I am just starting to learn django and I am facing the set-up phase. In particular I'd like to ask how to recognize if the virtual environment is activated or not. I know that I can use the command pip freeze but in all the tutorial that I am following, when the venv is activated, I can see the venv name in brackets in the terminal command line.
I can correctly activate the venv with the source command and check via the pip freeze command but I have no indication in the command line.
I am on a Mac/OS(M1) and using python3
thank you
To have visual information about the virtualenv on the command line you need to change the shell config to show it. It's not related to python or django itself.
It will depend on the shell that you are using, but assuming the default shell on mac you can check this question virtualenv name not show in zsh prompt
From venv docs.
When a virtual environment is active, the VIRTUAL_ENV environment variable is set to the path of the virtual environment. This can be used to check if one is running inside a virtual environment.
So you should be able to test it with:
import os
os.getenv('VIRTUAL_ENV') is not None
Yes, when the virtual environment is activated, it shows in the terminal as a prefix like
(env) yourname~Desktop/workspace-folder>
And that is enough to know it is activated, and you are using it.
Since you are using Python 3, you can create your virtual environment as follows in the same directory of your project, open terminal or iTerm and run this comman
python3 -m venv env
After few seconds, a folder env will be created that is your virtual environment. Note you can name your virtual environment as you wish, maybe like python3 -m venv djang-app
After you have created the virtual environment, you can now activate it like
source env/bin/activate
Again, if you created your virtual environment using lets say django-app
python3 -m venv django-app
You can activate it like this
source django-app/bin/activate
and you will see your terminal is prefixed as follows
(django-app) yourname~Desktop/workspace-folder>
To deactivate the virtual environment, you simply run this command deactivate in the terminal with active virtual environment.
You can learn more about python3 venv from here.

Best way to activate my conda environment for a python script on a windows PC

I've made a python script for multiple users and I want to be able to run it by just double clicking the script.
I also have made a conda env for this script and it seems I am unable to activate the environment within my python script (?).
I thought I could write a script which will activate my conda env and then run my python script in command prompt but I'm completely lost and can't find out how to even activate my conda env in command prompt.
Am I heading in the right direction with this - is the best way to activate a conda env and run a python script within one executable script via command prompt?
Sorry if this is a really obvious and/or stupid question, I am very new to all of this!
What I've tried so far:
I have now added conda and python to my path (thank you #Nesha25, I didn't need admin!). I then tried to run my script in the command prompt with: conda run -n Ngon_env python C:\Users\jlp\Desktop\Local_BLAST_scripts\Neisseria\ngon_script.py --live-stream But I get the following error which seems to occur after it tried to use the input function in my python script: EOFError: EOF when reading a line.
I then tried conda run -n Ngon_env --live-stream python C:\Users\jlp\Desktop\Local_BLAST_scripts\Neisseria\ngon_script.py and it doesn't seem to do anything and just gets stuck.
The conda run --no-capture-output flag worked instead, hooray!
Not a Windows user, but conda activate is for interactive shell sessions. The conda run command is for programmatic execution within an environment. So, you would have a script with a line like:
conda run -n my_env python your_script.py
or possibly
conda run -p /path/to/my_env python your_script.py
if trying to share the environment across users.
If the script requires interaction, you may need to add flags (like --live-stream and/or --no-capture-output). See conda run --help.

Virtualenv environment would not activate in CMD on Windows 10

I have tried this with python 3.7 as well as python 3.8.
Installed virtualenv with pip and then created a virtual environment with following command:
virtualenv abc
I cd to the Scripts folder and run the following command and please enter... the response is on the very next line:
C:\Users\user\abc\Scripts>activate.bat
C:\Users\user\abc\Scripts>
As you can see nothing happens... virtual environment does not get activated. I have also tried "Scripts\activate" from the environment folder.
I tried it with another windows machine that I have and its working fine there.
Any ideas?

How to activate virtual environment from Windows 10 command prompt

I'm trying to create and activate a virtual environment, using Windows 10 command prompt. I know that virtualenv is installed correctly, as the command
virtualenv venv
Works. I've navigated to my virtualenv download, Downloads\venv\Scripts, and am trying to activate my virtual environment venv. I've tried
venv activate
Which doesn't work since Windows doesn't recognize venv as a command. I've also tried
virtualenv venv activate
Which also doesn't work since virtualenv is saying that "venv activate" isn't a valid argument.
Use the activate script in the Scripts directory of your virtual environment:
> venv\Scripts\activate
This will activate your virtual environment and your terminal will look like this depending on the directory you're in:
(venv) C:\Users\acer\Desktop>
I hope this helps!
If you're using virtualenvwrapper-win, and using the DOS command prompt (as opposed to e.g. Powershell), then new virtualenvs are created using:
mkvirtualenv myenv
and activated using
workon myenv
You should define the environment variable WORKON_HOME to point to where you want you virtualenvs to reside.
If you've installed virtualenvwrapper-win>=1.2.4 then the virtualenvwrapper command will give you a list available commands:
go|c:\srv> virtualenvwrapper
virtualenvwrapper is a set of extensions to Ian Bicking's virtualenv
tool. The extensions include wrappers for creating and deleting
virtual environments and otherwise managing your development workflow,
making it easier to work on more than one project at a time without
introducing conflicts in their dependencies.
virtualenvwrapper-win is a port of Dough Hellman's virtualenvwrapper to Windows
batch scripts.
Commands available:
add2virtualenv: add directory to the import path
cdproject: change directory to the active project
cdsitepackages: change to the site-packages directory
cdvirtualenv: change to the $VIRTUAL_ENV directory
lssitepackages: list contents of the site-packages directory
lsvirtualenv: list virtualenvs
mkproject: create a new project directory and its associated virtualenv
mkvirtualenv: Create a new virtualenv in $WORKON_HOME
rmvirtualenv: Remove a virtualenv
setprojectdir: associate a project directory with a virtualenv
toggleglobalsitepackages: turn access to global site-packages on/off
virtualenvwrapper: show this help message
whereis: return full path to executable on path.
workon: list or change working virtualenvs
From the directory where you have your virtual environment (e.g. myenv)
you need to run the following command: .\myenv\Scripts\activate
Go to the folder where you have created the virtual environment in cmd and
enter the command .\venv\Scripts\activate
It will activate the virtual env in windows
from the command (cmd) prompt:
call venv/Scripts/activate
Make sure the Python Scripts folder is in your environment variables.
Usually the path is: "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\Scripts"
(Change "admin" to your windows username and "Python37-32" path according to your python version)
When you use "virtualenv" to create an env, it saves an "activate.bat" file in the scripts folder originating from the directory you ran the first command. E.g if you ran the command virtualenv env from C:/Users/Name/Documents/..., the .bat will be located in C:/Users/Name/Documents/.../env/scripts/activate.bat. You can run it from there.
Simply you can activate your virtualenv using command: workon myenvname
You can also create a command-line script like this -
#echo off
CD\
CD "C:\Users\[user name]\venv\Scripts"
start activate.bat
start jupyter notebook
Save this in a notepad file with an extension ".cmd".
You are ready to go
if you have anaconda installed then open anaconda terminal and type
> conda env list # for list of environment you already have
> conda activate {env_name} # to activate the environment
This works for me from Anaconda prompt,
.\\myvenv\\Scripts\\activate.bat
start python 3.7
python -m virtualenv
"You must provide a DEST_DIR"
python -m venv demodjango("demodjango is file name)"
activate.bat
pip install django
django-admin.py startproject demo1 (demo1 is my project)
python manage.py runserver
Performing system checks...
After doing this on a command prompt, you will get an URL. Click on that and you will see a message in the browser window that Django has been properly installed.

Categories

Resources