when should python3 object created in venv? - python

python newbie here.
I am on a windows machine and used Windows Subsystem for Linux(WSL) to execute commands.
I created my venv(folder) by entering:
python3 -m venv venv
and it would give me a new venv folder as below:
and then activated the virtual environment using:
source venv/bin/activate
however, I am having an issue as I executed another command and it is looking for python3 which should be present as in this path: venv/bin/python3
I am curious how should python3 created in venv. Thanks!

i guess you are confused about the Python virtual environment
first, the virtual environment is designed to establish a command execution environment for each project。
then. the first your code python3 -m venv venv call the system python command and the python3 should in /usr/bin/python3 and linked with /usr/bin/python3.x
but, if you activate the virtual environment of Python, the virtualenv will and the {you_dir}/venv/bin to the system path and you will call python through /venv/bin/python
to execute you python code.
there is no need create an link of /ven/bin/python3. but if you want, you can do it!

Related

Virtual environment (venv) gives a different python version from interpreter

In VS Code (Windows), my python interpreter points to version 3.11.
python -V in terminal gives me Python 3.11.0
I create a virtual environment with python3 -m venv virtual called virtual, and activate it with .\virtual\Scripts\activate.
Now in my environment, checking python -V gives me Python 3.9.13 instead.
How do I get venv to create a Python 3.11 environment?
You are creating virtual environment with python3 but checking version with just python. I want you to check if python and python3 are pointing same python executable file before creating virtual environment.
May be try creating virtual environment using just python, since its version is 3.11.0.
$ python -m venv virtual
Or you can create virtual environment with specifing the path of your python 3.11 executable file.
$ C:/path/to/your/python3-11-execuatable/python.exe -m venv virtual
In the view tab above click on Comand palette> python interpreter from there you can select python 3.11, otherwise you can add it by clicking on enter interpreter path and then select the path where python 3.11 is located at

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.

I am not able to activate my virtual environment in linux

I am not able to activate the venv available in my project directory.
When I do the following it still doesn't activate the venv and I am not able to use python or pip installed in my venv.
/my_project$ source venv/bin/activate
(venv) /my_project$
It does show that the venv is activated but when I check python and pip, version and location it shows that both are from the root dir usr/bin/python & usr/bin/pip.
Venv Installation Process
/my_project$ python3.10 -m venv venv
It's working well in my new directory and I am also able to activate the venv but my existing venv in the project folder that I created yesterday is not starting. I am new to Linux and don't know much about it but I believe it has something to do with the Linux reboot as after the reboot this started happening.
Any help would be appreciated. Thanks
OS: Ubuntu 20.04.4 LTS
system python: 3.8.10
python3.10: 3.10.5
perhaps try explicitly setting the python version when creating your venv:
i.e.
/my_project$ python -m venv venv python=python3.10
https://stackoverflow.com/a/61775880/14327910
and from (https://python.land/virtual-environments/virtualenv):
Blockquote
Python 3.4 and above
If you are running Python 3.4+, you can use the venv module baked into Python:
$ python -m venv [directory]
This command creates a venv in the specified directory and copies pip into it as well. If you’re unsure what to call the directory: venv is a commonly seen option; it doesn’t leave anyone guessing what it is.
A little further in this article, we’ll take a close look at the directory that was just created. But let’s first look at how to activate this virtual environment.

Unable to run python script in virtualenv using bash script

I want to run a python script in in-built anaconda environment tensorflow_p36. To check if it is in virtual environment or not, I am using command pip -V.
My first attempt at bash script:
#!/bin/bash
source activate tensorflow_p36
python /home/ec2-user/abc/temp.py
pip -V
Note: tensorflow_p36 being an in-built environment, does not require to be called from specific /env/bin directory. It can be activated from any directory. I think it's a feature of Amazon Deep Learning AMIs.
My second attempt at bash script:
#!/bin/bash
pythonEnv="/home/ec2-user/anaconda3/envs/tensorflow_p36/"
source ${pythonEnv}bin/activate
${pythonEnv}bin/python /home/ec2-user/abc/temp.py
pip -V
Note: When I try to run source /home/ec2-user/anaconda3/envs/tensorflow_p36/bin/activate command in terminal, the environment isn't being activated.
Each time, I am getting the same result:
pip 9.0.1 from /home/ec2-user/anaconda3/lib/python3.6/site-packages (python 3.6)
Whereas, I should be getting:
pip 9.0.1 from /home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages (python 3.6)
Can someone please explain how do I activate virtual environment and run a python script from that environment? I need to use this particular environment because of the dependencies installed in it.
Extra info:
Not sure if it matters, but the tensorflow_p36 is a conda environment, not a virtualenv.
This works with virtualenv. Create environment:
virtualenv -p python 3.6 tensorflow_p36
Then change the script to:
#!/bin/bash
source $HOME/tensorflow_p36/bin/activate
python /home/ec2-user/abc/temp.py
I believe the confusion has to do with the fact that you are using anaconda and not virtualenv to create a python environment. These two tools work differently.
If you are using an EC2 instance, why not to install tensorflow_p36 globally anyway?

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