.gitignore syntax - how to exclude virtualenv sub-directories?
Asked Answered
M

7

60

I have a couple different virtual environments in a project, e.g.

~/project_dir/sub_dirs/venv/...

Adding */venv/* to the .gitignore file does not prevent the virtual environment folders and their contents from being excluded. What is the correct syntax to prevent these virtual environment directories from being added to the git repository?

Mcgrew answered 17/2, 2017 at 20:0 Comment(1)
Is this question about how to ignore or about how to stop ignoring those directories?Abernethy
M
96

per https://github.com/github/gitignore/blob/master/Python.gitignore

venv/ does the trick for sub-directories.

Mcgrew answered 17/2, 2017 at 20:8 Comment(2)
Does this one work for any venv directory? Mine named tmp_env and it can't be ignored by this file, I still have to manually ignore it.Germanic
you have to add tmp_env/ in your .gitignore file.Verisimilitude
D
7
  1. Create in your current project's folder ".gitignore" file
  2. For example, if you have some folders as "venv" and ".vscode" than you just add their names in ".gitignore" file: venv/ .vscode/
Deracinate answered 23/11, 2019 at 16:32 Comment(0)
W
2

I tried everything to get this ignore to work, until I realized the easiest solution to this problem. Set up my virtual environment in a different directory outside the project!

Pycharm for example asks where you want to set up a virtual environment when you open a project with requirements. I just set it up outside in a different directory.

Wolcott answered 4/11, 2022 at 11:19 Comment(0)
R
1

If in Visual Studio, you can simple click in Team Explore -> right button in folder -> ignore

enter image description here

Require answered 31/12, 2021 at 12:21 Comment(1)
use this way but on pycharm and I found out that I fill wrong .venv instead /venv/ !!Chura
S
1

Inside the .gitignore folder just add the name of the folder with a slash at the end.

Example: venv/

This will ignore all sub directories in the venv folder.

Stacee answered 28/1, 2022 at 10:8 Comment(0)
S
0

I Had similar problem, But i'm trying to exclude 'vir-2.7/' which is not working. Seems the '.'(dot) was interrupted differently by git, so i have changed the directory name to 'vir_2_7', to fix it.

Sachi answered 26/11, 2018 at 2:30 Comment(0)
R
0

My virtual env was named as -venv. I included venv in .gitignore and it worked

Radiobroadcast answered 22/11, 2023 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.