How To Create & Activate A Virtual Environment In Visual Studio Code (Python 3.12)
Вставка
- Опубліковано 20 лип 2024
- In this video I will be showing you how you can create and activate a virtual environment for Python using Visual Studio code. It's good to know in case you are not using PyCharm since a lot of code editors will not have it created or activated by default.
▶ Become job-ready with Python:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels
00:00 Intro
00:10 Visual Studio Code
00:16 Project setup
00:34 Python 3.12
00:43 What if?
00:51 Error
01:15 Creating a virtual environment
01:48 Activating a virtual environment
02:24 Success!
03:28 One more thing…
03:49 It was that easy
03:54 Let me know what you think
Thank you, that is very helpful. Is there a way to activate the VENV automatically when a new terminal window is opened?
This helped me out a lot. Thanks!
This video was really helpful. Thanks!
love to watch your python videos :)
man thank you this is what im looking for.
thank you, this was simple and easy to follow
Love your content ❤
Excellent explanation thank you
Thanks... Great tutorial
Hello sir, when i use javascript in vs code and ctrl + s to save my code it will auto help me auto formatting, how can i do the same thing with python (.py file) in vscode
but how do you keep the venv actively when trying to run the app in debug mode, with the run button in the top right of vs code?
so would using pipenv shell be the same thing or are there any major difference?
Great tutorial
I prefer to use virtualenv instead of venv from a terminal/cmd/powershell, which is better and faster for some reasons. Some say it's the same, which is not the case at all..
So I've not tried virtualenv inside of VScode and give it a try.
Love you bro❤
Fantastic...
I made some python based tools for our team and had a git repo for it.
Everytime someone new joins, I needed to get them install needed libs and sometime conflicts happen.
Since I found venv, the issue is partially solved.
But now I face the issue on how to manage the venv with git.
A tutorial for that would be appreciated.
Thanks a ton :) Is there a way to activate the VENV automatically when a new terminal window is opened? (.venv) is not coming when we open Terminal after closing the IDE. @Indently Plz reply
It would be nice if you created Best Practices playlist and gather all this tips, like using __name__ == __main__, creating virt env when starting new project etc...
super helpfull, however one question i have is if I close vs code, how to I get back into the virtual environment(windows)
it doesn't see pip :( in VS code, even though I installed it in virt env as per tutorial
Personally, in vscode y prefer creating and activating a virtual enviroment using the "create environment" command
It could have been nice if you showed how to deactivate the virtual environment, great video btw ❤
I will write this down for the next tutorial!
I thought the same thing, just tried `exit` and it worked for me
legend
Useful content 👍. Venv is good as built-in feature. Personally i prefer Pipenv 😁
I never explored anything else other than the built-in; any particular reason your prefer pipenv?
@@Indently Pipenv is:
1. More automatic, one command (`pipenv install`) creates virtual environment and file with dependices
2. Just type `pipenv shell` to activate environment
3. New module installation adds depedicies automatically
4. Type just `exit` to deactivate environment
5. If You want clone virtual environment on other computer just paste depedicies file (`Pipenv.lock`) and type `pipenv sync`.
6. To remove environment just type `pipenv --rm`
7. You can create a requirements.txt file for venv
It is worth trying `pipenv`, at most it will not appeal to you.
Thanks for sharing, I will take a dive into it!
Excuse me, friend, I would like to ask you for help because when I try to create a virtual environment and write the python location in the terminal, I get the following error "Set-location: No position parameter found", just, I would like to know what mistake I made so that it does not detect it
For the virtual environment folder is ".venv" the standard? I have seen some projects use "venv" as the folder name
I know that the "." makes the folder invisible on Mac OS, but to be honest it's just what I've been doing since I started programming in Python. I would love to hear from someone else whether there's any benefit of doing that other than hiding folders for a cleaner directory.
The “.” is helpful for alphabetical ordering as well. “venv” gets mixed in with other folders while “.venv” is at the top. It also makes it more explicit that it is not meant to be part of the codebase but rather a supporting config type folder… similar to the .vscode, .git folders, etc.
You made it so simple that it instantly became a new muscle memory command...THANKS!!!
What are the advantages of using this virtual environment instead of regularly installing the package you want to use?
It helps you separate your code when working with same modules but of different versions, that's just one advantage
Actually, it's a great question.
The number of cases where the regular install is sufficient far outweighs the cases where venv is useful (plus it is clunky to use even with a great explanatory video like this).
...so I rarely use venv.
I would be interested to hear good use cases other than encapsulation of packages (which can be achieved with a simple requirements.txt file).
@vincentlaizer I agree with this, but still prefer to avoid...
The command for windows doesn't work for me. I've been trying to activate mine in vscode even before you uploaded this video. It says and error message "cannot be loaded be because the running script is disabled in this system"
Despite several stackoverflow questions, it doesn't help still.
Cmd command works fine but powershell doesn't.
Someone else correct me if I'm wrong since I don't have Windows, but it sounds like this is more a setting that has to do with the security settings on your computer?
@@Indently you're probably right too because it the same command I found on other places. I guess there are settings to be done to tackle the issue.
But the better solution is to use WSL with VSCode - solves many compatibility issues
Maybe it has something to do with execution policies. Run powershell as admin and try the command Set-ExecutionPolicy -ExecutionPolicy Unrestricted and try again the activate command.
@@alfredopalaciosolagaray7558 This helped so much, thank you!
Doesnt work
how to deactivate virtual environment in mac (in different shell)
Here are the commands:
Mac OS:
python3 -m venv .venv
source .venv/bin/activate
Windows:
python -m venv .venv
.\.venv\Scripts\activate
(Please correct me if I'm wrong for Windows, I will update this comment)
in vs code you can create and activate virtual environment using command panel too
In Windows Activate goes with capital "A" -> .venv\Scripts\Activate
I have just done it yesterday. Depending on which shell you use it might be correct.
For example in Powershell you need to ".\.venv\Scripts\Activate.ps1" and also enable scripts in powershell. But scripts in PS is another topic
.\.venv\Scripts\Activate
You might want to run the windows command to check the .venv directory since I believe that "python -m venv .venv" and "py -m venv .venv" produce different folder structures in .venv
Continuing with this topic, it's worth mentioning the use of pip freeze > requirements.txt to generate a list of installed packages and their versions and the subsequent installation of these packages from the generated requirements.txt file.
@@squishy-tomato
👍Certainly, Poetry offers distinct advantages over pip. However, for beginners and a general understanding of virtual environments and package installation for their projects, using pip is entirely sufficient.
This is nice, but please continue with Flet and create a real application.
I will upload plenty of them, but it's not the only thing I will upload on this channel ;)
Next video: How to deactivate venv. (Congratulations for a great channel)
just type deactivate on windows
know already
Impressive!
You didn’t really describe why virtual environments are helpful. Could be useful to describe using a requirements file in conjunction with a virtual environment. My preference for managing my virtual environments and dependencies is Poetry.
I actually didn't describe why they are helpful at all, but I covered how to create and activate them. I will consider the rest for another tutorial :)
Can you do a tutorial running a python virtual environment inside docker please, I'd like to use it as my development environment. Thanks.
We do this at work. Our conclusion was that inside docker a virtual environment makes no sense, because you are already in an isolated environment.