Python Forum
Keeping up with IDEs and Virtual Environments...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keeping up with IDEs and Virtual Environments...
#1
Hello Everyone,

I am trying to understand how to use virtual environments and different IDEs without problems...Let me start by explaining my current understanding of virtual environments: a virtual environment is a smart way to isolate projects from each other. I think of it as a "folder" that contains dependencies, scripts, as well as a specific version of the Python interpreter. Here my questions:

a) Should every virtual environment always contain a Python interpreter along with modules and the scripts that we want to run? I guess so since it would not make sense to have a virtual environment without a Python interpreter.

When working with virtual environments, the steps are:
- create a virtual environment
- use pip to install the Python interpreter as well as the required modules

b) How do we then use our IDE of preference(assuming we have several on our computer) to write scripts that will use the Python interpreter and modules living inside the virtual environment? Do we launch the IDE and and activate the specific virtual environment from it?

c) Anytime we download an IDE (PyCharm, Thonny, Jupyter, etc.) are we automatically downloading another Python interpreter? If we use multiple IDEs, how do we keep everything synced up so that when I switch IDE and write scripts with it I can still import the installed modules? I tend to think that every IDE uses its own interpreter and can use only the modules that were installed through it because they are saved in a specific folder where the IDE looks for them...

Thank you!
Reply
#2
You cannot have a virtual environment without a Python interpreter. A virtual environment is nothing more than the Python interpreter and the installed packages. Your project is not really part of the virtual environment. Your Python project uses the virtual environment instead of the system Python.

Quote:When working with virtual environments, the steps are:
- create a virtual environment
- use pip to install the Python interpreter as well as the required modules
Missing a step. You need to activate the virtual environment before using pip to install packages.
Correction. You don't use pip to install Python. You can use pip to install the tool used to create virtual environments, but you never pip Python. You can also make virtual environments without using pip at all.

Quote:b) How do we then use our IDE of preference(assuming we have several on our computer) to write scripts that will use the Python interpreter and modules living inside the virtual environment? Do we launch the IDE and and activate the specific virtual environment from it?
Your scripts are not part of the virtual environment unless you installed them. For example, you could pip install some scripts into your environment
pip install -e folder_containing_scripts
But mostly you use a virtual environment to configure the things your program will use, and you use project layout to "install" the files you are working on. Think of the venv as being mostly static, and you project as having all the stuff that changes.

Quote:c) Anytime we download an IDE (PyCharm, Thonny, Jupyter, etc.) are we automatically downloading another Python interpreter?
IDE's do not include Python. You need to tell an IDE which Python to use. If you tell it to use a virtual environment, it will use that as your Python. I have not used Jupyter. Don't forget VSCode which is my favorite IDE.

Virtual environments are not tied to an IDE. You can use a virtual environment without any IDE. You can use the same virtual environment for multiple projects. You can use different IDE's and have them using the same virtual environment.
bytecrunch and ndc85430 like this post
Reply
#3
Thank you!

I think I am missing a piece about virtual environments: we don't use pip to add a Python interpreter to the virtual environment. So how do we connect a specific Python interpreter version to a virtual environment? What are the commands?

Ok on IDEs non coming with an interpreter. I guess the IDLE did when I downloaded it. It seems that the downloading of an IDE generally comes with downloading an interpreter so we can run the scripts we write. For example, if I downloaded PyCharm and that was my first and only IDE, I bet I could also run the scripts I write which implies that an interpreter is working behind the scenes...

Thanks!


(Sep-04-2022, 02:26 PM)deanhystad Wrote: You cannot have a virtual environment without a Python interpreter. A virtual environment is nothing more than the Python interpreter and the installed packages. Your project is not really part of the virtual environment. Your Python project uses the virtual environment instead of the system Python.

Quote:When working with virtual environments, the steps are:
- create a virtual environment
- use pip to install the Python interpreter as well as the required modules
Missing a step. You need to activate the virtual environment before using pip to install packages.
Correction. You don't use pip to install Python. You can use pip to install the tool used to create virtual environments, but you never pip Python. You can also make virtual environments without using pip at all.

Quote:b) How do we then use our IDE of preference(assuming we have several on our computer) to write scripts that will use the Python interpreter and modules living inside the virtual environment? Do we launch the IDE and and activate the specific virtual environment from it?
Your scripts are not part of the virtual environment unless you installed them. For example, you could pip install some scripts into your environment
pip install -e folder_containing_scripts
But mostly you use a virtual environment to configure the things your program will use, and you use project layout to "install" the files you are working on. Think of the venv as being mostly static, and you project as having all the stuff that changes.

Quote:c) Anytime we download an IDE (PyCharm, Thonny, Jupyter, etc.) are we automatically downloading another Python interpreter?
IDE's do not include Python. You need to tell an IDE which Python to use. If you tell it to use a virtual environment, it will use that as your Python. I have not used Jupyter. Don't forget VSCode which is my favorite IDE.

Virtual environments are not tied to an IDE. You can use a virtual environment without any IDE. You can use the same virtual environment for multiple projects. You can use different IDE's and have them using the same virtual environment.
Reply
#4
(Sep-04-2022, 03:35 PM)bytecrunch Wrote: I think I am missing a piece about virtual environments: we don't use pip to add a Python interpreter to the virtual environment. So how do we connect a specific Python interpreter version to a virtual environment? What are the commands?
You are talking about venv that comes with Python?
Usually use the lasted version have on OS,but can use any version that is installed.
Example:
G:\div_code
λ python -V
Python 3.10.5

G:\div_code
λ python -m venv new_env

G:\div_code
λ cd new_env\

G:\div_code\new_env
λ G:\div_code\new_env\Scripts\activate

(new_env) G:\div_code\new_env
λ python -V
Python 3.10.5 
Let say i want to use Python 3.7 that have installed,then can use py(comes with Python).
G:\div_code
λ py -3.7 -V
Python 3.7.3

G:\div_code
λ py -3.7 -m venv new_env

G:\div_code
λ cd new_env\

G:\div_code\new_env
λ G:\div_code\new_env\Scripts\activate

# Environment will now use 3.7
(new_env) G:\div_code\new_env
λ python -V
Python 3.7.3
There are 3-party packages that can install a new Python version to a environment(not on OS) like Anacondas conda ,
or Poetry with pyenv on Linux.
Poetry is really good so worth to take a look.

Quote:Ok on IDEs non coming with an interpreter. I guess the IDLE did when I downloaded it.
No,but PyCharm will usually find any Python version if there is any on the OS.
Look at Configure a Python interpreter PyCharm
There see how to choice an exiting interpreter or virtual environment.
Reply
#5
@snippsat Don't you think Poetry is a bit advanced for someone really new to Python?

@bytecrunch The way to "attach" a particular Python to a virtual environment will depend on the tools used to make the virtual environment.
Reply
#6
(Sep-04-2022, 05:23 PM)deanhystad Wrote: @snippsat Don't you think Poetry is a bit advanced for someone really new to Python?
Maybe,but is as easy to use as venv.
So as venv is build in that's fine for starting out.
Also if just have a grasp how virtual environments works that is enough,
then can concentrate on learning Python.
Reply
#7
One day, I installed a module, I can't even remember what it was. While it was installing, I noticed that it installed a version of pillow.

After that, im.show() would not work, don't know where the problem lies.

from PIL import Image
# pics is a list of image names
for pic in pics:
    im = Image.open(pic)
    im.show()
I can now only do this in a venv

I got this advice from https://realpython.com/python-virtual-en...-a-primer/

Quote:Made a directory in /home/pedro called PVE. Go there in bash then:

1. python3 -m venv OMRenv (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) to deactivate the virtual environment.
4. You can run IDLE within the activated virtual environment with this command:
python -m idlelib.idle (works well)

Just these last few days I needed to do stuff with a lot of photos, so I made a directory /home/pedro/images

I go to the folder images then activate and the bash prompt changes to this:

Quote:(images) pedro@pedro-HP:~/images$

Quote:pedro@pedro-HP:~/images$ source images/bin/activate
(images) pedro@pedro-HP:~/images$ deactivate
pedro@pedro-HP:~/images$
Reply
#8
@Pedroski55 you should make a your own Thread.
As the topic is kind of similar i don't move Thread for now.

What you doing is confusing,you make folder /home/pedro/images
Do you make this folder with mkdir or with venv?
When you use venv its making a new folder.

I would do it like this,i always end _env so know that folder is used for virtual environment.
# Make 
G:\div_code
λ python -m venv image_env

# cd in
G:\div_code
λ cd image_env\

# Activate 
# Linux <source /bin/activate>
G:\div_code\image_env
λ G:\div_code\image_env\Scripts\activate

# install Pillow
(image_env) G:\div_code\image_env
λ pip install pillow
Collecting pillow
  Downloading Pillow-9.2.0-cp310-cp310-win_amd64.whl (3.3 MB)
     ---------------------------------------- 3.3/3.3 MB 6.2 MB/s eta 0:00:00
Installing collected packages: pillow
Successfully installed pillow-9.2.0

(image_env) G:\div_code\image_env
At this point can make images folder in image_env folder.
Or navigate to where you have images as long as (image_env) is activate,
no matter what folder you are in it will use Python from this environment(that has Pillow installed).
Example:
(image_env) C:\Users\Tom
λ cd C:\foo\images

# In different folder as activate (image_env) will use python executable from this environment 
(image_env) C:\foo\images
# Test what python interpreter used,see it point to image_env
λ python -c "import sys; print(sys.executable)"
G:\div_code\image_env\Scripts\python.exe 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best practice on using virtual environments in Python bytecrunch 6 863 Feb-14-2024, 03:22 PM
Last Post: snippsat
  Virtual Environments - Organization (VS Code) JaysonWonder 11 1,869 Jan-26-2023, 11:34 PM
Last Post: Larz60+
  I don't understand pip and environments snakes 3 1,249 Jul-31-2022, 08:17 PM
Last Post: snakes
  Keeping a value the same despite changing the variable it was equated to TheTypicalDoge 2 1,468 Mar-13-2022, 10:50 PM
Last Post: Yoriz
  Use different Anaconda environments on Linux Mint and Spyder StaLLoNe_CoBRa 0 1,891 Jan-20-2021, 03:12 AM
Last Post: StaLLoNe_CoBRa
  pip and venv virtual environments soupworks 2 2,307 Dec-30-2020, 11:38 PM
Last Post: soupworks
  Traceback error in PyCharm but not in other IDEs? devansing 7 6,384 Mar-05-2020, 11:27 AM
Last Post: buran
  Problem keeping the window up benlyboy 11 4,172 Jan-24-2020, 02:12 AM
Last Post: tamilselvisubbiah
  Virtual Environments pneveux 1 1,846 Apr-04-2019, 09:15 PM
Last Post: Larz60+
  merging lists, dedup and keeping order, in older pythons Skaperen 3 3,177 Oct-19-2018, 01:30 AM
Last Post: ODIS

Forum Jump:

User Panel Messages

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