69

(Probably a noob question, but I didn't find a solution after googling for 20 minutes.)

I created a new pure Python project with PyCharm which yielded the following folder structure

myproject
└── venv
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── easy_install
    │   ├── easy_install-3.5
    │   ├── pip
    │   ├── pip3
    │   ├── pip3.5
    │   ├── python
    │   ├── python3
    │   └── python3.5
    ├── include
    ├── lib
    │   └── python3.5
    ├── lib64 -> lib
    └── pyvenv.cfg

Where do I put myproject.py or the myproject folder now?

  • Inside or outside of venv?
  • In the venv/binfolder?
  • Just inside venv, i.e. myproject/venv/myproject.py?
  • 15
    Outside of your venv folder. – DariusFontaine Jul 24 '18 at 13:33
  • I think @tripleee 's answer is enough to answer this question, but I recently has a problem when I put my python files in the `myproject` folder because I was making an exe file with it (using pyinstaller). I would suggest in case someone has similar problems, `Scripts` folder would be the place to store your files. – Ankit Chawla Apr 06 '21 at 11:11

2 Answers2

62

The virtual environment manages files which aren't yours. It doesn't care how you manage your own files. Put them wherever makes sense to you, just not anywhere inside the venv directory tree. Common solutions include directly in myproject, or in myproject/src.

For what it's worth, one of the important use cases for virtual environments is the ability to delete one and start over. You obviously can't do that if you put stuff there which isn't part of the virtual environment. Regard them as ephemeral infrastructure.

Another use case is the ability to have multiple virtual environments for the same project, so that you can test that your code works with different versions of the libraries you depend on, or even different Python versions.

A common convention is to collect the libraries you need in requirements.txt so that you can create a new virtual environment, activate it, and pip install -f requirements.txt whenever you need to.

tripleee
  • 175,061
  • 34
  • 275
  • 318
16

I guess you misunderstood the term "Virtual Environment". It provides an isolated environment wherein you can download a different version of python packages and run it for your project. Hence, do not put anything inside your virtual environment. Keep it clean.

To take advantage of the virtual environment,

  • activate it (source path_to_virtual_env/bin/activate )
  • install the necessary python packages using pip (pip install XYZ)
  • and run your python code using python command (python3 mycode.py)
JR ibkr
  • 869
  • 7
  • 24
  • 1
    Python doesn't have JARs. That's Java. – jwodder Jul 24 '18 at 14:01
  • Oops thanks for pointing it out. I will correct it to python packages. – JR ibkr Jul 24 '18 at 14:02
  • 5
    This does not really answer my question. – problemofficer - n.f. Monica Jul 24 '18 at 15:13
  • 2
    I am aware of that, but it helps you to understand the concept itself. If you get the concept then it will automatically answer all of the questions regarding the virtual environment. – JR ibkr Jul 24 '18 at 15:21
  • 1
    @JRibkr if your answer does not attempt to answer the question, please leave it as a comment. Better yet, include a comment with a link to a reputable explanation of Python virtualenvs. – 0xdd Sep 28 '18 at 19:40
  • 19
    I think confusion arises by what is meant by a virtual environment. When I first came across python venv, I was thinking it might be like a VMWare virtual env or a Docker container: with these you definitely go into the env or container to do anything, you can't code outside it. But with python venv you keep your own source code files outside of the venv. The venv is more like an env as in a sourced bash env - it sets up PATHs etc. So it was not obvious to me at first whether my own source code files had to be inside venv. Now it's clear that they must NOT be inside venv. My 2c. – Will Mar 01 '19 at 20:20
  • [1] So it does mean, that venv allows me to install a couple of different python versions. Thereby, it allows me to test those different versions for compatibility with my source code right? [2] I should leave the venv file untouched? [3] Do i need to add venv files to my git repo? – Ndrslmpk Jan 30 '22 at 13:28
  • A virtual environment primarily lets you manage the required libraries in isolation from the rest of your system. If you have multiple Python versions installed on your system, you can create a venv for each, so that you can test your project with different Python versions, for example. You should generally not check your virtual environments into version control, though you migth want to check in `requirements.txt` (which customarily lists the required libraries for a project) – tripleee Feb 05 '23 at 17:56