Clean New Projects with venv - Virtual Environments

NeuralNine · Intermediate ·💻 AI-Assisted Coding ·2y ago

Key Takeaways

The video demonstrates how to create and manage virtual environments for Python projects using the venv module, allowing for isolated package installations and version management. It covers creating virtual environments, activating and deactivating them, installing packages, and exporting package lists to requirements.txt files.

Full Transcript

what is going on guys welcome back in this video today we're going to learn how to professionally set up virtual environments for new projects in Python so let us get right into it [Music] alright so oftentimes we're going to be working on multiple different python projects at the same time and those different python projects rely on different packages and oftentimes on different versions of the same packages so for example I might have a data science project that is a little bit older and it relies on Panda's version 1. so pandas one point something and I might have a newer version or a newer project which relies on pandas version 2. something and those are incompatible and on my system I can only have one pandas installation so when I open up the terminal for example and I type pip3 list or to have it in the requirements txt format pip3 freeze you can see that each package has exactly one version number I cannot have pandas one point something and pandas two point something installed because when I install one of them the other one is going to be uninstalled so for example if I say now pip3 install I think I have pandas1 installed here because of a project that I'm working on without an environment and if I say now I want to install pandas equals equals version 2. something uh then it's going to uninstall the installation that I already have as you can see here uninstalling pandas 1.5 to install pandas 2. uh 2.0.3 and the same thing the other way around if I install pandas1 it's going to uninstall pandas2 and because I need both versions depending on the project the best practice way to do that the professional way to work with multiple projects is to create so-called virtual environments so for this I'm going to go to my desktop let's say here now I have two projects let's say project one and I have project two and instead of just using my python installation and all the packages that I have on my system I'm going to create now virtual environments for those two different projects and how do I do that there is a core python tool called vnf that we can use to create these virtual environments now I also have a video already on this channel on Virtual enf which is a third party tool so a package that is not a core python package and this is useful if you're working with older python versions but since python 3.3 there is this uh core python module vnf available and this is the go-to way to work with virtual environments so let's say this is now the project one here we can open up the terminal and we can create a new virtual environment and the idea of a virtual environment is that you have specific package versions and only those packages are installed in that particular environment so you don't get all the packages you have on your system you only get the packages that you install for that virtual environment and you only get the package versions that you specify here inside of that virtual environment and this is useful for multiple reasons first of all of course because you can have different versions of pandas for example for the different projects but second of all you can also just export everything that is installed in the environment into and into a requirement txt file so that people can easily install everything that you have in your virtual environment to reproduce uh the setup basically so there is no real convention for naming the environment you can call your environment whatever you like however the I wouldn't say convention but the go-to way to do that is to create an environment inside of a project folder and to call it either vnf Dot N or Dot vnf and I think that the dot VN version is actually recommended in the python documentation so we're going to go with this one so to create a virtual environment with vnf we're going to say python or Python 3 depending on your terminal and operating system and then we're going to say Dash M vnf and then the name of the environment in our case Dot vnf and as you can see now here it creates a DOT vnf directory in my project folder and what I have to do now is I have to navigate into that directory here and then I'm going to just say here on Linux Source bin and then uh Source bin and then activate and this is going to activate the virtual environment now if I say pip 3 list you will see that I have exactly two packages installed here pip and setup tools so basically nothing and if I say now pip3 install pandas it's going to install the most recent version of pandas and you will see here in a second this is pandas2 so pane is 2.0.3 and if I now say deactivate in this environment uh and if I now say pip3 list and I grab the pandas line you will see that on my main installation without a virtual environment I have pandas one but if I do the same thing here again with activate and then the same command you can see I have pandas 2.0.3 installed so I have different pandas versions depending on the environment and of course I can do the same thing now I can deactivate again and I can go back to the desktop I can go to project uh project two and here I can create another environment with the same name so Python 3 vnfindin Dot vnf and in here for example I do the same thing Source dot vnf bin activate and then I can just say pip 3 install pandas equals equals one point whatever and then it installs a different pandas version in this virtual environment so depending on which virtual environment I'm in I'm going to be working with different packages and of course here I can also now install something else like for example uh opencv Dash python and the good thing about such a virtual environment is now that I can go ahead and Export everything I can take all the installed modules with the versions that are installed and I can export them to a requirements txt file so pip3 freeze is what I want to get this is the text and I can just take this and feed it into a requirements txt file and then I can open this file and you can see it is what we have installed here in this virtual environment so when I create some project here and I want to pass this to someone to tell them look this is what I have and if you want to run this on your systems on your system those are the packages that you need to have installed they can just run pip3 install Dash R requirements txt and they will if they have their own virtual environment they can then reproduce the exact setup that I have here at least on a python level so of course system packages are not included here but this is also a benefit of virtual environments now another way to see that you're in a virtual environment is to see where python is so you can say which Python 3 and you can see that the Python 3 that we're currently accessing here is part of the virtual environment this is not the case if I deactivate in this case you will see that it's in user bin python 3. so yeah this is how you work with virtual environments using the core python module vnf so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye

Original Description

In this video we learn how to properly create new Python projects with virtual environments and venv. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 💼 Services 💼 💻 Freelancing & Tutoring: https://www.neuralnine.com/services 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to create and manage virtual environments for Python projects using venv, allowing for isolated package installations and version management. It covers creating virtual environments, activating and deactivating them, installing packages, and exporting package lists to requirements.txt files. By following these steps, developers can ensure consistent and reproducible project setups.

Key Takeaways
  1. Create a new virtual environment using venv
  2. Activate the virtual environment
  3. Install packages using pip
  4. Export package list to requirements.txt file
  5. Deactivate the virtual environment
💡 Virtual environments allow for isolated package installations and version management, ensuring consistent and reproducible project setups.

Related Reads

📰
I Grepped My Own Claude Code Logs and Found the Hidden Tag Anthropic Never Shows You
Discover a hidden tag in Claude Code logs by grepping your own JSONL transcript, revealing undocumented features.
Dev.to · Sho Naka
📰
Dockered Claude: keeping research code compilable but obscured from my AI agent
Use Docker to keep research code compilable but obscured from AI agents, ensuring proprietary models and methods remain secure
Dev.to AI
📰
Import AI 466: The bitter lesson for robotics, AIs complete week-long programming tasks; and OpenAI's accidental AI hacker
Learn how AI systems can complete week-long programming tasks and the implications of MirrorCode, a benchmark for long-horizon programming tasks
Import AI
📰
The Junior Developer Pipeline Is Broken... And AI Broke It
AI is disrupting the junior developer pipeline, making it harder for new engineers to enter the field, and it's crucial to address this issue to ensure the future of software development
Dev.to · Nazar Boyko
Up next
How to Create a WordPress Custom Login Plugin Using Claude AI (Step-by-Step)
Quick Tips - Web Desiign & Ai Tools
Watch →