virtualenv Env not creating bin directory in Windows 7 - python

I'm a newbie to Python and I've spent hours on this. I can't seem to figure out why when I run a simple command to setup my Python environment: virtualenv --distribute env
This doesn't create a bin file in the env directory.
It only creates:
-- env
-- Include
-- Lib
-- Scripts
My impressions was that a bin directory would be created per a lot of the examples I've found on the web (e.g. I'm not able to run this command: env/bin/activate).
I'm using Windows 7 and Python 2.7.

On Windows, this is entirely correct. A bin directory is created on POSIX systems only. From the Windows Notes section of the documentation:
Some paths within the virtualenv are slightly different on Windows: scripts and executables on Windows go in ENV\Scripts\ instead of ENV/bin/ and libraries go in ENV\Lib\ rather than ENV/lib/.
For Windows, run \path\to\env\Scripts\activate to activate the virtualenv. From the documentation again:
On Windows you just do:
> \path\to\env\Scripts\activate
And type deactivate to undo the changes.
Based on your active shell (CMD.exe or Powershell.exe), Windows will use either activate.bat or activate.ps1 (as appropriate) to activate the virtual environment.

If you're using Git Bash for Windows, I found the following command works to activate the environment: $ source (path to environment)/Scripts/activate
I was using Git Bash on Windows 10, Python(v3.7), Pip (v19.0.3), virtualenv (v16.4.3)

Yes, correct. On Windows, it creates the directory hierarchy as follows:
venv (virtual environment name)
Include
Lib\site-packages
Scripts
Just like in this image:
.

For windows , you have to run python -m venv <your_env_name> and
source <your_env_name>/Scripts/acivate.
This works for me on windows.

Related

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.

From a Python venv, find installation from which the venv command was run

Given a Python virtual environment created with pythonXY_EXE -m venv venv,
how do I find the python executable PythonYX_EXE used to create the virtual environment?
Solutions for both MS-Windows and Linux are sought.
For Linux, I can come up with a solution by resolving the symlink with readlink -f venv/bin/python.
But for MS Windows?
A non-answer is the official Creation of virtual environments:
... pyvenv.cfg file in it with a home key pointing to the Python installation from which the command was run
My goal: Running python without copy/symlink, as those may fork multiple processes.
On MS Windows, by inspecting the source code of the venv library, the executable is located at (if symlinks are not enabled or failed)
<home-key-of-pyvenv.cfg-file>/python.exe.

Where is python interpreter located in virtualenv?

Where is python intrepreter located in virtual environment ?
I am making a GUI project and I stuck while finding the python interpreter in my virtual environment.
Execute next code and it will print location of your python interpreter.
import sys
print(sys.executable)
To a lot of extents, it depends on how the virtual environment was created. If you installed like this py -m venv env then you would find it here .\env\Scripts\activate . How ever if you created the environment using conda then you would find it here Anaconda_installation_folder/envs/name_of_environment. SO brief answer to your question, you would find it withing you env folder.
If you want to automatically active virtual env or (venv). Try this.
Install virtual env
python3 -m venv venv
If you have already have venv present. you can refer use this to get the executable python3 file like this.
#referred #Arty answer above.
import sys
print(sys.executable)
this will print the python3 path of that venv.
later open command pallet ( ctrl + shift + P ) and type python as shown in image.
Then click on
and paste that path into the input box.
Now the setup is done.
Now every time you open a terminal, it will automatically open your terminal in the virtual env, and will auto-run the virtual env for you.

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.

python virtualenv does not use correct version of python

I'm creating a Django app that requires me to use python2.7.6 . My system has python3.4.1 installed so I have to use a virtualenv with python2.7 installed. I installed such a virtualenv using Pycharm and named it django_python_2.7 but when I activate it in the terminal and run "python", it still shows that it's using system's python3.4.1:
here is what I did:
Activate the environment:
source django_python_2.7/bin/activate
Run python, and it shows:
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21) ---> this is the system level python and not the one installed in virtualenv
However, when I run which python, it shows the correct path that points to virtualenv's python version:
/Users/calvinmwhu/....../django_python_2.7/bin/python
When I explicitly run the python version installed in that virtualenv:
django_python_2.7/bin/python
it shows the correct version:
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
I have no idea what's going on. I'm developing this app in Pycharm IDE but I really like executing commands in the terminal . But in the terminal the virtualenv is not using the correct version of python..Why does running a simple "python" command in the virtualenv still default to the system's python ?
Could anyone provide some hints? Is it necessary to change the PATH variable to make it contain the path to the virtualenv's python?
If you want to change the PYTHONPATH used in a virtualenv, you can add the following line to your virtualenv's django_python_2.7/bin/activate file
export PYTHONPATH="/path/to/python"
export OLD_PYTHONPATH="$PYTHONPATH"
To restore to its original value on deactivate, you could add following line to your django_python_2.7/bin/postdeactivate script.
export PYTHONPATH="$OLD_PYTHONPATH"
Otherwise, create new env using
virtualenv -p /usr/bin/python2.7 django_python_2.7
I discovered the same problem...
and like #skyline75489 mentioned:
I forgot that i had stated an alias to my python3 executable a time ago.
I found it in my .bash files in my home directory and removed it.
Everything worked out fine again with my virtual environment.
If you changed the path to your venv or ranamed any of the parents folders of your venv directory, then this will break the configured paths, if that is case you have two options:
recreating it
Create a requirements.txt file using: pip freeze > requirements.txt
Delete the venv directory: rm -r old-vnev/
Create a new virtualenv with correct name: python -m venv new-venv
Activate new virtualenv: source new-venv/bin/activate
Install packages from requirements.txt: pip install -r requirements.txt
Another simpler way
search for all occurences of the string old/path/to/your/venv/
replace them with correct/path/to/your/venv/
after that source new-venv/bin/activate will work as intended again.
Hope this help!
In case it helps anyone else: if you changed the path to your venv folder (such as changing the parent folder), this will happen. This was my issue.
Recreating your virtualenv will fix it, as you should hopefully have a requirements.txt created to rebuild your virtualenv.
This might have even been the root cause for OP.
Double check your paths. I had an issue like this recently where running which python from within the activated virtualenv would still return the default system version (/usr/bin/python). However, if I ran the scripts specifying the binaries directly (./venv/bin/python, etc) from within the virtualenv, it worked as expected so it appeared all the dependencies had been installed correctly.
The issue was that I had moved the parent virtualenv directory after building everything. This meant all the virtualenv paths pointed to the original location which was no longer valid, and python correctly defaulted to the default system binary.
I use a bash script like this:
$ source venv/bin/activate
$ alias vpython=$VIRTUAL_ENV/bin/python3
and use vpython when wanting to use the python executable within the virtual environment. A nice way to check which executable you are actually using within python is the following:
>>> import sys
>>> print(f'executable \033[0;33;40m{sys.executable}\033[0m')
In my situation after system update symbolic link from the virtualenv was somehow broken and it switched to default system python version. The solution was to replace symbolic link by the correct one.
Deactivate virtual env if you are inside by:
deactivate
Change virtualenv python symbolic link:
ln -s /your/wanted/python/bin/python /your/virtualenv/bin/python
Start virtualenv again and it should use correct python version.
If you are not sure where is your python, then you can localise it by:
which python3
I had a similar problem. But I had it because I had moved my env folder to another place. So, if you did so, just go to activate file in bin folder and change VIRTUAL_ENV="CurrentPathToYourEnvFolder" (it's 40th line in file)

Categories

Resources