Where do I put my python files in the venv folder?

36,905

Solution 1

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.

Solution 2

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)
Share:
36,905

Related videos on Youtube

problemofficer - n.f. Monica
Author by

problemofficer - n.f. Monica

Updated on July 09, 2022

Comments

  • problemofficer - n.f. Monica
    problemofficer - n.f. Monica almost 2 years

    (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?
    • DariusFontaine
      DariusFontaine almost 6 years
      Outside of your venv folder.
    • Ankit Chawla
      Ankit Chawla about 3 years
      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.
  • jwodder
    jwodder almost 6 years
    Python doesn't have JARs. That's Java.
  • JR ibkr
    JR ibkr almost 6 years
    Oops thanks for pointing it out. I will correct it to python packages.
  • problemofficer - n.f. Monica
    problemofficer - n.f. Monica almost 6 years
    This does not really answer my question.
  • JR ibkr
    JR ibkr almost 6 years
    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.
  • 0xdd
    0xdd over 5 years
    @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.
  • Will
    Will about 5 years
    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.
  • Ndrslmpk
    Ndrslmpk about 2 years
    [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?