2

Should I create a directory in my virtualenv and save the source code for that specific project there?

Thanks in advance

Abhijith Konnayil
  • 4,067
  • 6
  • 23
  • 49
AnriSonohara
  • 55
  • 2
  • 5
  • Related: https://stackoverflow.com/questions/1783146/where-in-a-virtualenv-does-the-custom-code-go – dat Dec 03 '22 at 23:40

2 Answers2

7

You're not required to have them related in any way. As long as your env is activated, it doesn't matter where it is.

But what's usually easiest is:

  • Create an empty directory for your project.
  • Create the virtualenv under that project directory.
  • Put the source in a src directory under that project directory (or, for really simple projects, just drop it in the project directory).

That way you can check the env settings into version control alongside your code, and if you use an auto-venv tool it'll activate the env every time you cd into the project directory, and so on.

As a side note, if you're just learning this stuff today, you might want to consider learning pipenv instead of using raw virtual environments.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
abarnert
  • 354,177
  • 51
  • 601
  • 671
1

You can save your project anywhere. But you should activate the virtual environment before working on that project.

You can activate the virtual environment using the following command.

source path-to-virtualenvironment/bin/activate

After activating the virtual environment move to your project location.

Abhijith Konnayil
  • 4,067
  • 6
  • 23
  • 49
  • How will my .py file know to use libraries in my virtual env? – AnriSonohara Jul 21 '18 at 19:57
  • 1
    Once you activate the virtual environment, you can install the packages that you need for your project. All that packages will be installed in the virtual environment. Once the virtual environment is activated your project takes libraries from the virtual environment. – Abhijith Konnayil Jul 21 '18 at 20:00
  • You can deactivate the virtual environment, once you finished working on that project using the command `deactivate` – Abhijith Konnayil Jul 21 '18 at 20:04