remove virtual environment created with venv in python3

Viewed 101890

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.

5 Answers

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.

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

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 }}"

Related