0

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 ?

enter image description here

The Great
  • 7,215
  • 7
  • 40
  • 128

2 Answers2

3

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.

phd
  • 82,685
  • 13
  • 120
  • 165
felipe
  • 7,324
  • 2
  • 28
  • 37
  • Thanks for the response. Upvoted. but When I type `env/bin/deactivate`, I get this error message `/bin/sh: 1: .: Can't open env/bin/deactivate`... Is it because there is no `deactivate file`? – The Great Jun 22 '20 at 08:07
  • Sorry, fixed answer. `deactivate` is a variable that is available to you after your source the `activate` file. – felipe Jun 22 '20 at 08:10
  • Sorry, am new to Python and I don't understand what you are saying. My Apologies. I was following a tutorial and actiavted a virtual env and now I would like to deactivate it.. As I don't have the `deactivate` file under my `env/bin`, what should we do now? I didn't make any changes or delete files under `bin` folder. Why is that file missing? – The Great Jun 22 '20 at 08:10
  • Check comment response above (in this thread, posted 15secs before you). – felipe Jun 22 '20 at 08:10
  • Again, however: `virtualenv` is **not** intended to be used inside Jupyter notebook. Not only that, it will **not** work as I think you hope it will. – felipe Jun 22 '20 at 08:11
  • Just trying to understand your comment (posted 3 mins ago), are you saying that since I didn't use `source` keyword to activate my `virtualenv`, I can't use `de-activate` keyword/variable? Only way to de-activate is to reload the browser/session? – The Great Jun 22 '20 at 08:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216407/discussion-between-the-great-and-felipe). – The Great Jun 22 '20 at 08:19
  • Can help me with this post? https://stackoverflow.com/questions/62530145/os-getenv-returns-empty-output-python – The Great Jun 23 '20 at 08:05
0

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

Alexander Kosik
  • 669
  • 3
  • 10
  • Hi @Alexander Kosik, When I type `env/bin/deactivate`, I get this error message `/bin/sh: 1: .: Can't open env/bin/deactivate`. – The Great Jun 22 '20 at 08:05
  • Is it because there is no deactivate file under my `bin` folder? – The Great Jun 22 '20 at 08:06
  • Check manually if the deactivate file is present in `python-virtual-environments/env/bin/deactivate` which must be the case and call it with a path prepended depending on your current location – Alexander Kosik Jun 22 '20 at 08:07
  • No the file isn't present. – The Great Jun 22 '20 at 08:08
  • Sorry - can't explain why the file is missing. Perhaps try it from console and check the result. – Alexander Kosik Jun 22 '20 at 08:11
  • 1
    @AlexanderKosik `deactivate` is a shell function that is sourced in part by `env/bin/activate` -- not a file in itself. If you were to `source env/bin/activate` in the terminal, you would simply do `deactivate` to deactivate it -- not run/source another shell script. Hopefully that clears it up :) – felipe Jun 22 '20 at 08:13
  • In my world it's a separate file if I use venv ;) – Alexander Kosik Jun 22 '20 at 08:15
  • I just tried `python3.8 -m venv test-venv` and no, there is no `deactivate` in `test-venv/bin/`. The answer is wrong, downvote. – phd Jun 22 '20 at 13:50
  • Maybe it is depending on the os? With Win 10 and python 3.8.0 if have a deactivate. tested with `python3.exe -m venv venv`. I can also check on unix os later... – Alexander Kosik Jun 22 '20 at 13:52
  • Yes, it is os depending. My answer is only valid for windows os. – Alexander Kosik Jun 22 '20 at 13:57