I already referred this related post but it doesn't help. Please refer the details below
I was learning about python virtual environments and was trying to execute some basic commands.
Though I am able to activate the virtualenv, I am not able to de-activate it.
The jupyter notebbok file is currently in path /home/abcd
Below is what I tried
!mkdir python-virtual-environments
!cd python-virtual-environments
!virtualenv env
!. env/bin/activate # here `source` didn't work. So, I replaced it with `.` and it started working
!. deactivate # doesn't work. I tried `! deactivate` but it doesn't work
I get the below error
**`/bin/sh: 1: .: deactivate: not found`**
I have two questions
a) How to deactivate the virtualenv that I created env? What's the proper command?
b) Why is the env folder created in my present working directory? Shouldn't it be under python-virtual-environments based on mkdir and cd commands ?
How to deactivate the virtualenv that I created env? What's the proper command?
Simply restart the kernel - that should do it. Or, simply do deactivate.
Why is the env folder created in my present working directory? Shouldn't it be under python-virtual-environments based on mkdir and cd commands?
Every time you use the ! command, you are creating a new shell that executes the command -- in other words, when you cd you are going into the folder, but the next command is back in root. Therefore, you can do:
!mkdir python-virtual-environments && cd python-virtual-environments && virtualenv env && source python-virtual-environments/env/bin/activate
It should be mentioned, however, that this does not actually activates the virtualenv inside the Jupyter notebook, as that's simply not how virtualenvs work. Virtual environments will hold the actual Python executables with all the related pip installable packages. Sourcing it via the notebook won't do much unless you are later on calling Python via !python command.
For Windows (tested with Windows 10):
deactivate is in the same folder als your activate script.
So to deactivate also use it with a prepended path: env/scripts/deactivate
Related
I'm a beginner to Django and Python, and I've never used virtualenv before. However, I do know the exact commands to activate and deactivate virtual environments (online search). However, this learning course takes time and sometimes I need to split the work over 2 days.
When I create a virtualenv today and do some work, I'm unable to access the same virtualenv tomorrow. Even when I navigate to that folder and type in .\venv\Scripts\activate, it says "system cannot find specific path".
How can I open already existing virtual environments and the projects within them? Could it be that I need to end my previous session in a certain way for me to access it the next time?
Even though pipenv had so many problems. I suggest you use it when you are new to virtual env.
Just
pip install pipenv
cd $your-work-directory
pipenv shell
Then you created your project env.
You can active it by:
cd $your-work-directory
pipenv shell
You can install packages by:
cd $your-work-directory
pipenv install $yourpackage --skip-lock
Open the command prompt
Go to the project directory, where you created virtual environment
And type the same as error shows, as in my case it was
File D:\Coding files\Python*recommendation-system\venv\Scripts\activate.ps1* cannot be loaded because running scripts is disabled on this system.
So I typed recommendation-system\venv\Scripts\activate.ps1
And it resolved my problem.
Use this and it will work:
cd $working directory u have virtual env on
pipenv shell
you can use this command it worked for me for reusing your existing venv
$ workon then the name of your existing venv
In my project, I use virtualenv created the ENV:
(ENV) bora-MBP:testDemo01 ldl$ ls
ENV db.sqlite3 manage.py templates test01 testDemo01
You see I have an active virtual environment of the project.
In one python file of my project:
import six
print(six.PY3) # print True
I checked the python version of my project using, there it shows it uses Python3.
but I cd into the ENV/lib, there only shows python2.7:
(ENV) bora-MBP:testDemo01 ldl$ cd ENV/lib/
(ENV) bora-MBP:lib ldl$ ls
python2.7
there is no python3, this is my first question.
My second question is, why my project will use the ENV virtual environment? where is the configuration I can check?
EDIT-01
In my first question:
I know I'm using python3 in my project now, I can use six.PY3 to check, or other ways, but why I create a virtual environment, there only gets python2.7 directory under the ENV/lib/?
My second question:
we know my project will use the ENV environment to run program, but why? is there any default settings for my project to use this environment(dependencies, packages and so on)? is there any configuration file for us to check(clearly point out my project testDemo01 will use the ENV as run environment)?
For your first question:
Please check python version on your virtual environment using python --version to make sure it is python 3 or not. Check whether you have multiple versions of python using whereis python in the command line.
For your second question:
To make a virtual environment management process easier install virtualenvwrapper pip install virtualenvwrapper-win.
Then you will be able to check and change your ENV directory from 'edit the system environment variables'. Change or create 'WORKON_HOME' variable with your require path.
Based on your EDIT-01:
It seems like you have multiple version of python installed in your machine and virtualenv is using python2.7. Please use virtualenv -p path_of_python3 envname to create virtualenv with python3. You can find path for all python using whereis python in the command line.
You can check your dependencies using pip freeze inside your virtualenv.
First question, python2.7/ under ENV/lib/ is because of the creation virtualenv method, if you create the ENV like this:
virtualenv -p python3 ./ENV
there should be python3.x/ under ENV/lib/.
For the second question:
is there any configuration file for us to check(clearly point out my project testDemo01 will use the ENV as run environment)?
for the source reason, you should check the ENV/bin/active bash file, when you active the virtual env, then the project use that yet:
VIRTUAL_ENV="/Users/aircraft/Desktop/TestPython/Demo/venv"
export VIRTUAL_ENV
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH
# unset PYTHONHOME if set
if ! [ -z "${PYTHONHOME+_}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
unset PYTHONHOME
fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="$PS1"
if [ "x" != x ] ; then
PS1="$PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
fi
export PS1
fi
you see, when you execute the active bash, there will export the VIRTUAL_ENV, and add it into the PATH, and unset PYTHONHOME if set. This is why the project will use the virtualenv.
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.
I am trying to run virtualenv for the first time and when I run the following code the shell prompt does not change.
$ virtualenv test
New python executable in test/bin/python
Installing setuptools............done.
$ source test/bin/activate
$
I'm assuming it did not activate my environment because there is no (test)$ prompt. The virtualenv command creates the directory correctly, so the activate file is there, it just seems to not be working?
I have tried this command as well and it gives the same result.
$ . test/bin/activate
$
Is my virtual environment running and I just cannot see the prompt(and is there something I can run to check that)?
or
Is my virtual environment simply not running and I am not activating it correctly?
I'm using Python 3.6.0(installed from Homebrew), virtualenv 15.1.0, Mac OS X 10.11.6, and zsh(oh-my-zsh)
To tell if your virtualenv is active without depending on the prompt (which shell configuration can override in persistent ways), use:
declare -p VIRTUAL_ENV
If the output is:
declare: no such variable: VIRTUAL_ENV
...then your virtualenv is genuinely not active. By contrast, if the output is:
typeset -x VIRTUAL_ENV=/Users/jess/test
...then it's active, but your prompt has been overridden.
To diagnose this (either the failure to activate, or the prompt override not sticking) in zsh, use:
PS4=':%x:%I+' # the equivalent to this in bash would be PS4=':$BASH_SOURCE:$LINENO+'
set -x
source test/bin/activate
...which will print source file, line number, and command executed for everything done both during and after the activate script is sourced; this should include the activate script updating your PS1 (or, potentially deciding for some reason not to execute); and may include a function hook changing your PS1 back to a version that doesn't have the prompt updated to show the virtualenv's state.
Thanks to the help from Charles, I discovered it was my oh-my-zsh theme(powerlevel9k) that was just not displaying the prompt. I simply needed to add in the virtualenv Python Segment in my ~/.zshrc described here.
If you are using oh-my-zsh on iterm and want to prompt virtual environment then ,
Do open ~/.zshrc Add/update virtualenv in that file like this POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context time battery dir vcs virtualenv custom_wifi_signal) Then finaly don't lforget to source ~/.zshrc
I have problem with virtualenv when working in macOS Sierra, it affect only one of the work folders, that make it very weird for me to get why is happening, when setup the new virtualenv I'm designating folder for it and install it there, after activating it all is working fine till I deactivate the virtualenv or restart the system, than I'm getting this:
~/Development/DjangoDEV/FlowersVibeShoppe/venv-flowersvibe-oscar on master
$ source bin/activate
-bash: bin/activate: No such file or directory
If I try to list the existing virutalenv it showing it there:
~/Development/DjangoDEV/FlowersVibeShoppe/venv-flowersvibe-oscar on masterâ–²
$ lsvirtualenv
venv-flowersvibe-oscar
The virtualenv is there, however don't want to activate, I did uninstalled and setup again from scratch like 4 times the result is always the same for this folder, and this affect only one of the work folders the rest of my virtualenv are working fine. Can some one HELP?
Thanks
lsvirtualenv is a command from virtualenv-wrapper, not from virtualenv itself. That project has a separate command for activating virtualenvs: workon. So you should do:
workon venv-flowersvibe-oscar