53

How can I delete a virtual environement created with

python3 -m venv <name>

Can I just remove the directory?

This seems like a question googling should easily answer, but I only found answers for deleting environments created with virtualenv or pyvenv.

McLawrence
  • 4,975
  • 7
  • 39
  • 51
  • Yes, deleting the directory should be just fine. I have done it multiple times, and re-created them without any problems whatsoever. – DatHydroGuy May 24 '17 at 12:40
  • Possible duplicate of [How do I remove/delete a virtualenv?](https://stackoverflow.com/questions/11005457/how-do-i-remove-delete-a-virtualenv) – Santhosh Mar 08 '18 at 10:52

5 Answers5

71

Yes, delete the directory. it's where executables for the venv and modules and libraries and entire other stuff for venvs is kept.

gonczor
  • 3,994
  • 1
  • 21
  • 46
  • 18
    delete _which_ directory? – StingyJack Feb 13 '19 at 05:31
  • 2
    The one containing your virtual environment. If you create one with `env my-env`, the directory to delete would be `my-env` – gonczor Feb 13 '19 at 09:04
  • 1
    @gonczor But that's where my source lives. – Cees Timmerman Apr 20 '20 at 07:25
  • 1
    @CeesTimmerman what do you mean? It's a place for libraries, not the code you've written. If you create a virtual env with `virtualenv venv`, then `venv` stores external dependencies, not your application code. – gonczor Apr 20 '20 at 11:02
  • 2
    @gonczor That could've been more clear. Also that `python3 -m venv venv` [replaces](https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe) `virtualenv` and other older methods. – Cees Timmerman Apr 20 '20 at 15:14
  • @StingyJack for Ubuntu the directory is "/home/username/.local/bin/.virtualenvs". Go inside this folder and you will find the folder to be deleted. This folder has the same name of your virtual environment. – hafiz031 May 30 '20 at 00:56
9

If your environment is active, you should deactivate it first. Not sure if not deactivating will cause any problems, but that's the right way to do it. Once you deactivate, you can simply delete the virtual environment directory.

To deactivate an active environment, simply execute the 'deactivate' bash command.

Tushar Vazirani
  • 1,011
  • 13
  • 14
  • Which directory? There are multiple. – Cees Timmerman Apr 20 '20 at 07:39
  • @CeesTimmerman The directory doesn't matter. The environment is mentioned before the command prompt in brackets: (my_env_name). You can deactivate if you want to keep the terminal session running, you can also close all terminal sessions that are running the virtual environment. – Ronald van Elburg Nov 24 '22 at 10:29
7

In your venv project folder created using python3 -m venv . or whatever, run this to remove the venv files:

rm -r bin include lib lib64 pyvenv.cfg share

If you're still in the venv by using source bin/activate, run deactivate first.

However, according to this page, one should always use python3 -m venv .venv so the venv files are neatly contained in a single .venv folder in your project root. That way the Visual Studio Code Python extension can find/use it as well.

Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124
2

To delete a environment in WINDOWS. Make sure you are in activated environment:

$ deactivate

This will deactivate your current environment. Now you can go to the directory where your folder or folder is present. Delete it manually. DONE!

To create a new environment , Simply from bash:

$ python3 -m venv venv

To activate it:

$ source venv/bin/activate
Dhrumil Panchal
  • 406
  • 5
  • 11
-1

There is no built-in way to remove a virtualenv created with python3 -m venv <name>. If you created a python3.6 virtualenv in, for instance, /usr/local then you can remove it with an Ansible playbook like:

---
- name: Remove virtualenv
  hosts: all

  vars:
    venv: /usr/local

    virtualenv_files:
      - pyvenv.cfg
      - bin/activate
      - bin/activate.csh
      - bin/activate.fish
      - bin/easy_install
      - bin/easy_install-3.6
      - bin/pip
      - bin/pip3
      - bin/pip3.6
      - bin/python
      - bin/python3
      - bin/python3.6
      - bin/wheel
      - lib/python3.6/site-packages

  tasks:

  - name: Freeze virtualenv
    shell: |
      set -e
      source "{{ venv }}/bin/activate"
      pip3 freeze > /tmp/frozen
    args:
      creates: /tmp/frozen
    register: frozen
    failed_when: false

  - name: Remove site-packages from virtualenv
    when: frozen.rc == '0'
    become: true
    shell: |
      set -e
      source {{ venv }}/bin/activate
      pip3 uninstall -y -r /tmp/frozen

  - name: Remove virtualenv_files
    become: true
    file:
      path: "{{ venv }}/{{ item }}"
      state: absent
    loop: "{{ virtualenv_files }}"

bbaassssiiee
  • 6,013
  • 2
  • 42
  • 55