71

(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?
2
  • 16
    Outside of your venv folder. Jul 24, 2018 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. Apr 6, 2021 at 11:11

2 Answers 2

63

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.

0
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)
8
  • 1
    Python doesn't have JARs. That's Java.
    – jwodder
    Jul 24, 2018 at 14:01
  • 5
    This does not really answer my question. Jul 24, 2018 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, 2018 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, 2018 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 1, 2019 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.