#πŸ”’ How to use venv?

75 messages Β· Page 1 of 1 (latest)

left fable
#

Hello! I am very new to Pythong(I do have some Java knowledge), and I've been learning the basics now.
I came across virtual environment the other day, but have been struggling to understand how to use it?
I managed to create one and activate it, but was then having issue with importing a class from another file.
Once I deleted the venv, i worked fine.

When I create a venv, do I create my python files in it?
Which command prompt do I use - the VS Code one, or Windows CMD? I seem to have less issues with the Windows CMD.

woven doveBOT
#

@left fable

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.

brittle prawn
#

create one
python3 -m .venv (this is the path) venv (this is the command)

#

then you activate it using
source .venv/bin/activate

#

basicaly its a container like object that works like a sperate JVM

#

where the enviroment settings and libaries are contained in the .venv folder

#

as your often running python on OS that depend on python you dont want to screw with the base python intall of the OS

#

python3 -m (this is telling python to make a new .venv

#

if you got somethign like vs code it will offer to make a venv for you

honest ibex
#

the command would be python3 -m venv [path] so python3 -m venv .venv
python3 calls python while -m tells it to interpret the next argument as a module to run. venv is the module you would be running here while finally you would supply it a path (i.e. .venv) where you want to create the venv.

You also don't necessarily need to create your .py files in the same directory as your venv (though it is common practice to create the venv in the root folder of your project)

teal swift
#
root
|_.venv
|_main.py
#

Where main can be any name

left fable
#

How to use venv?

teal swift
#

Oh...
I wanted to make a joke about that

honest ibex
#

After you activate it the venv will replace your default python/pip with it's own version (only until the end of your terminal session) (meaning that if you pip install something it will not affect python in other places on your system)
So if you then run your main script (python3 -m main it will be run using that environment instead of your systems global one.

left fable
teal swift
#

No it's too late

left fable
#

So basically the venv is just a place where I can mess with the version/settings, without affecting my other projects?

#

I feel like I generally enjoy learning the language itself so far, its these things around it that annoy me

honest ibex
#

Less the settings and more what kind of libraries you install.

But yes, its main use is to seperate your workspace and provide a "clean" base to work on to aviod version/dependencie issues between different projects

brittle prawn
#

its simlar to JVM with its own engine execution optiosn

#

where you clone your base JVM then alter/use it

#

99% of the time its added to .gitignore

left fable
#

So I am currently in VS Code, and have the console open

#

in order to start a venv, I would type python3 -m .venv

#

on windows*

brittle prawn
#
source .venv/bin/activate```
`
#

is what you do to create and activate

left fable
#

Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

#

when I tried that

teal swift
#

maybe py

#

Instead of python3

left fable
#

Yes, py it is

brittle prawn
#

this is why i use devcontainers

#

πŸ€·β€β™€οΈ

honest ibex
#

In order to create a venv (a seperate directory) you use python3 -m venv <path>
In order to activate it you would use <path>/Scripts/activate (slightly differs depending on operating system)

teal swift
#

Also if you're on windows it's not source

honest ibex
#

its activate.ps1 (for powershell) on windows

left fable
#

So in order to activate it here, what would it be?

honest ibex
#

.venv\Scripts\activate.ps1 maybe?

teal swift
#

Should be

#

Vs code used PowerShell iirc

left fable
honest ibex
#

It says PS at the start of the line, so yeah

left fable
#

how can I check if its activated? I tried running the .bat instead

honest ibex
#

It tells you that you need to change your Execution policies.
Honestly I hat this behaviour on windows. 😑 I know why it is there but Linux would never do this to you

#

This is basically Winows telling you "You didn't explicitly tell me to trust you so I will not follow your instructions to run code of untrusted sources"

left fable
#

Riiiiiiiiiiiiight

#

thats why when I run it through CMD, I probbably run it with elevated rights and it works fine

honest ibex
#

I'm rusty on my windows stuff but it might be limited to powershell and not extend to cmd (just a guess)

#

The easiest fix would be to just change your Execution policies

left fable
#

I would love to get familiar with Linux, but would hate to learn 2 things at once πŸ˜„

#

I would need to dig in how to do it

teal swift
left fable
#

I had a separate SSD with that idea, but coudln't get Linux working and gave up on it

#

Okay, I changed the Policy and I think it worked

#

Now in terms of file locations:

Is that okay? Or would the py files need to be in venv?

honest ibex
#

No, like this is the preferred way.
(For bigger projects you usually group them into seperate directories and only leave a single main.py in the root folder)

formal apex
#

To be more explicit: your source code does not belong in the venv, this is a little Python environment which contains a Python and third party packages you may want for your project. You should consider it disposable πŸ™‚

honest ibex
#

Treat the .venv directory like that it should not harm your project at any time if you deleted it.
That includes documenting installed libraries in a pyproject.toml or requirements.txt file to quickly recreate your current venv

left fable
#

Understood, thank you very much!

left fable
#

Right, here is the issue I had last time:

#

I tried importing the Classes on top:

#

But when I run it, I get this:

teal swift
left fable
#

Spot on! Im so used to auto save..

teal swift
#

Vs code doesn't have auto save

#

I think PyCharm Community (aka. the free version) has auto save

#

I'm not sure tho

left fable
#

Thats okay, I will get used to it

woven doveBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.