#๐Ÿ”’ Creating virtual environment for existing large scale project.

24 messages ยท Page 1 of 1 (latest)

outer crow
#

I've been working on a large scale project for some time now without using a virtual environment. I've never used a virtual environment for a project and don't really know how they work.

I'm getting to the point in the project where I'm starting to use some packages that aren't part of the standard library. So:

  • Would it be advisable to start using a virtual environment? Why/why not?

  • If so, how would I go about converting the existing project to one?

midnight sparrowBOT
#

@outer crow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

novel carbon
#

sure, you should use a virtual environment. Why? Because if you ever create a second, unrelated Python project on that computer, you can keep the two projects' dependencies separate

#

it's trivial to create a virtualenv: ```
$ cd my-project
$ python3 -m venv .venv

#

then use .venv/bin/pip install to install the dependencies, and .venv/bin/python whatever.py to run your code

outer crow
#

What would my folder structure look like afterwards?

#

i.e. I currently have the project arrayed as such:

/src # Contains project directory and related resources for the project
  /pvz-preview # Contains the project source itself
    __main__.py # Project entry point
```I currently run the project for `/src` with:
```ps
py -m pvz-preview
```How would this change when using a venv?
outer vector
#

When venv is activated, py knows to use it as first choice. You can do py -0 and see then that it will list python from the venv as what if will be using by default

#

Activating a venv also adds venv stuff to PATH, so you can also use python and pip directly then.

Just make sure your venv is active

#

!venv we also have some explanation why venvs are useful

midnight sparrowBOT
#
Virtual environments

Virtual environments are isolated Python environments, which make it easier to keep your system clean and manage dependencies. By default, when activated, only libraries and scripts installed in the virtual environment are accessible, preventing cross-project dependency conflicts, and allowing easy isolation of requirements.

To create a new virtual environment, you can use the standard library venv module: python3 -m venv .venv (replace python3 with python or py on Windows)

Then, to activate the new virtual environment:

Windows (PowerShell): .venv\Scripts\Activate.ps1
or (Command Prompt): .venv\Scripts\activate.bat
MacOS / Linux (Bash): source .venv/bin/activate

Packages can then be installed to the virtual environment using pip, as normal.

For more information, take a read of the documentation. If you run code through your editor, check its documentation on how to make it use your virtual environment. For example, see the VSCode or PyCharm docs.

Tools such as poetry and pipenv can manage the creation of virtual environments as well as project dependencies, making packaging and installing your project easier.

Note: When using PowerShell in Windows, you may need to change the execution policy first. This is only required once per user:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
outer crow
outer vector
#

Outside the src, presumably that would be some main directory of the project.

Venv itself is not part of source, it should never be uploaded anywhere (just the requirements file gets uploaded, so that everyone can recreate the venv), that's why it doesn't go inside /src

outer crow
#

I've seen in some places that all the source code files go inside the venv folder. Is that the case? Or is the venv folder separate to that?

outer vector
#

No, source will never be inside the venv. The venv will contain the packages you install via pip, so it will contain those packages' files - maybe that's what you've seen? Those will mostly be .py files

outer crow
#

One site showed their main.py file inside at the top-level of the venv folder, separate from the libraries folder.

outer vector
#

Nope, don't do that please, don't put anything into the venv folder directly, only use pip for changing the content of it

outer crow
#

Ok, thank you, is there any way to still reference the global python installation for packages, or do they all need to be in the venv? For example pandas will be a commonly used package, so could I have that installed in my global installation, or do I need to install it for every venv?

outer vector
#

From py -m venv --help:

--system-site-packages Give the virtual environment access to the system site-packages dir.
๐Ÿ™‚

#

I gotta go now, my phone will soon die and I should get to sleep anyways.
Good luck!

outer crow
#

Thanks, getting late for me too!

outer crow
#

!close

midnight sparrowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.