1

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!

4
  • 1
    The location of a venv is irrelevant since a venv is just activated or deactivated. It should be clear if you make one yourself, activate it and check the output of "which python". Commented Sep 25, 2020 at 10:20
  • And can I also put the python files themselves in the venv? Commented Sep 25, 2020 at 15:02
  • Why do you need to put them in the same folder? Most allow you to specify the folder where to store the virtual env files. See the syntax for venv in Creating Virtual Environments. Though, you usually don't need to do this (or even care about it), Just set the virtual env up and activate it every time you work on the project. If you are using source control, you won't be committing the virtual env files anyway, so there's no advantage to putting it together with project files. Commented Sep 25, 2020 at 16:09
  • I wrote a blog about this hope it helps (liuhongbo.medium.com/…) Commented Mar 8, 2023 at 12:49

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.