Python Forum
Best practice on using virtual environments in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best practice on using virtual environments in Python
#1
Hello everyone,

I am curious to know how you others use virtual environments in their daily Python life....Virtual environments are an important topic. I am a Windows user this is what I think is the correct sequence of steps:

a) Create a regular Windows folder calling it Project01.This folder contains the .py scripts and the virtual environment folder itself Each different project should have its own folder containing related scripts and its dedicated virtual environment.

b) Next we open the Windows terminal (CMD or Powershell), use the folder full path to go to the folder Project01 where the virtual environment folder will be located, type python -m venv ven01 to create a virtual environment called ven01 inside the folder Project01.

c) A the command line, type ven01\Scripts\activate to activate the environment.

What to do from here? Now I have an activated virtual environment....I can open an code IDE at that point...How do I ensure that the code I write (the .py script) is connected to the activated virtual environment?

I know some IDEs (VS code, etc.) have their own terminal/shell so we don't need to use CMD but I would like to know how to proceed from there to better understand the inner workings....

THANK YOU
bytecrunch
Reply
#2
Not a Windows user, and not a frequent venv user (too lazy to start it for the little things I do!)

But I think you have got the gist of things!

I have a little home-made OMR programme. Once, after installing a module, my OMR programme had a problem. The new module I installed installed a different or modified version of PIL. No OMR!

Then I had to make a venv according to instructions I found on the web for Linux. The point being, the modules you install in the venv are only for use there.

I made a directory in /home/pedro called PVE (it is about 300MB). cd there in bash then:

1. python3 -m venv OMRenv (i.e. python3 -m venv new_directory_name)
2. In bash: source OMRenv/bin/activate then you are working within the venv
3. Type deactivate in bash when you see (OMRenv) in bash to deactivate the virtual environment.
4. You can run IDLE within the activated virtual environment with this command:
python -m idlelib.idle (works well)

I really only ever use Idle for making and trying Python. From Idle in your venv you can open anything you are working on from anywhere and run it, modify it, save it. But in the venv you only use modules which you install there, maybe newer versions or maybe older versions.

After installing PIL and other modules I needed in my venv, my OMR worked again!

This is my images venv seen from bash and starting Idle:

Quote:pedro@pedro-HP:~$ source images/bin/activate
(images) pedro@pedro-HP:~$ python -m idlelib.idle
bytecrunch likes this post
Reply
#3
I make virtual environments for each large project I do so I can control the packages. For little day to day things I have a catchall venv that I use, adding packages when I need them for the work at hand. Occasionally I might delete the environment and make a new one. I never use the installed python distributions directly.
bytecrunch likes this post
Reply
#4
(Feb-11-2024, 03:24 PM)deanhystad Wrote: I make virtual environments for each large project I do so I can control the packages. For little day to day things I have a catchall venv that I use, adding packages when I need them for the work at hand. Occasionally I might delete the environment and make a new one. I never use the installed python distributions directly.

Thank you. But after you create a virtual environment and also activated it, how do you make sure that your IDE (whatever that may be, IDLE, Pycharm, VS code, etc.) uses that activated environment? Do we need to launch the IDE we plan to use from the command line, by typing its name, right after we activated the environment and see the (ven1) on the command line?

Thanks
Reply
#5
(Feb-12-2024, 11:40 PM)bytecrunch Wrote: Thank you. But after you create a virtual environment and also activated it,
VS Code, PyCharm will detect the environment or just choice from a list of Interpreter.
Example.
# Make 
G:\div_code
λ python -m venv my_env
# cd in
λ cd my_env\
# Activate
λ G:\div_code\my_env\Scripts\activate
# Start VS Code
G:\div_code\my_env
(my_env) λ code .
As i start VS Code from environment,it will usually auto detect the environment,see 3.12('my_env': venv).
This work the the same for Anaconda/Miniconda as you have ask about before,
also poetry(stable mature now) is good choice if making a package(share on eg PyPi).
[Image: Yy5aq8.png]
bytecrunch likes this post
Reply
#6
(Feb-13-2024, 12:32 AM)snippsat Wrote:
(Feb-12-2024, 11:40 PM)bytecrunch Wrote: Thank you. But after you create a virtual environment and also activated it,
VS Code, PyCharm will detect the environment or just choice from a list of Interpreter.
Example.
# Make 
G:\div_code
λ python -m venv my_env
# cd in
λ cd my_env\
# Activate
λ G:\div_code\my_env\Scripts\activate
# Start VS Code
G:\div_code\my_env
(my_env) λ code .
As i start VS Code from environment,it will usually auto detect the environment,see 3.12('my_env': venv).
This work the the same for Anaconda/Miniconda as you have ask about before,
also poetry(stable mature now) is good choice if making a package(share on eg PyPi).
[Image: Yy5aq8.png]

Great! Thank you. All clear now: enter the activated environment and launch the IDE, like VS code....I am sure we can go the other way: launch VS code and activate the virtual environment from the VS Code terminal...which is also a terminal...

What is different between the VS Code terminal and the regular Windows terminal like CMD or Powershell?

Jupyter notebook, another IDE, has these things call kernels that makes things less clear. Jupyter is a client-server application that runs on a single machine locally. What is the kernel about in this case?
Reply
#7
(Feb-13-2024, 11:17 PM)bytecrunch Wrote: What is different between the VS Code terminal and the regular Windows terminal like CMD or Powershell?
I would advise to use cmder (use Download Full).
In VS Code can choice what Terminal to use,eg image under you see that i use cmder.
Ofen so do use cmder shell alone,an not the integrated in VS Code.
[Image: 9poGCK.png]
Quote:Jupyter notebook, another IDE, has these things call kernels that makes things less clear. Jupyter is a client-server application that runs on a single machine locally. What is the kernel about in this case?
It's just to use it from environment where you have install Jupyter Notebook.
So in this example(my_env) i could have done pip install jupyterlab.
Then would it look like this if start Notebook from environment.
G:\div_code\my_env
(my_env) λ jupyter lab
Just remember if use this way or Anaconda/Miniconda,there is no common Jupyter Notebook you install into a environment and use it from there.
So just find a ok setup that work,when i use Notebook i use a Miniconda Environment where i have installed most common packages that is needed
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Virtual Env changing mysql connection string in python Fredesetes 0 386 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Installing python packages in a virtual environment Led_Zeppelin 1 776 Aug-15-2023, 08:18 PM
Last Post: deanhystad
  Virtual Environments - Organization (VS Code) JaysonWonder 11 1,869 Jan-26-2023, 11:34 PM
Last Post: Larz60+
  Keeping up with IDEs and Virtual Environments... bytecrunch 7 2,535 Sep-05-2022, 08:04 PM
Last Post: snippsat
  I don't understand pip and environments snakes 3 1,250 Jul-31-2022, 08:17 PM
Last Post: snakes
  How do I link the virtual environment of that project to the 3.9.2 version of python? Bryant11 1 1,382 Feb-26-2022, 11:15 AM
Last Post: Larz60+
  install apache-airflow[postgres,google] on Python 3.8.12 virtual env ShahajaK 1 7,877 Oct-07-2021, 03:05 PM
Last Post: Larz60+
  best practice for import libraries and using pyinstaller aster 3 2,892 Apr-17-2021, 11:12 AM
Last Post: snippsat
  Virtual environment and upgrading python 3.5 to 3.9 NeilUK 4 12,178 Jan-24-2021, 01:02 PM
Last Post: snippsat
  Use different Anaconda environments on Linux Mint and Spyder StaLLoNe_CoBRa 0 1,892 Jan-20-2021, 03:12 AM
Last Post: StaLLoNe_CoBRa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020