Daniel F.
Daniel F.

Reputation: 95

Confusion about python virtual enviroments

I have recently started learning python. The learning is going very quickly, because I have got previous experience with programming. There is one thing though, which confuses me a lot, and it is the virtual enviroments. Sometimes in a video I will see or hear someone talking about setting up a venv, but I dont entirely understand everything about it.

I know that they keep the dependencies for specific projects, because some projects might need different modules than others. But can I also put the project files themselves in the same folder as the virtual enviroment? Do I have to create the virtual enviroments in a specific place/folder?

It would be nice if someone could maybe help me figure this out a bit!

Upvotes: 1

Views: 143

Answers (1)

Niko Fohr
Niko Fohr

Reputation: 33770

The location of python virtual environments does not matter. You can create the virtual environment folder either into centralized place, like ~/venvs/myproj, or you can have them inside your project folder, like

myproj
|-- venv/
|-- setup.py
|-- somemodule/
|-- .gitignore

And of course, any other folder is a valid place for a python virtual environment, but these two are the most common practices.

If the virtual environment is placed inside the project folder, it should not be added to version control. Rather, one would list the project dependencies in setup.py, requirements.txt, pyproject.toml or similar. These serve just instructions on how to create the virtual environment, and that is all you need to save.

Also note, that virtual environments are not intended to be modified manually; what I mean is that sometimes beginners might want to write their own code inside the venv folder, and that is not how virtual environments are meant to be used. Own code ("project code") should be placed separately from the virtual environment. One should think that it is OK to delete any virtual environment anytime, since you have the instructions (setup.py, requirements.txt, pyproject.toml) to rebuild the virtual environment with just one command all the time.

Upvotes: 2

Related Questions