Python Forum
Do I need to make a virtual environment? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Do I need to make a virtual environment? (/thread-29774.html)



Do I need to make a virtual environment? - Pedroski55 - Sep-19-2020

I run Ubuntu 18.04

I read I can create a "virtual environment" for Python 3 with:

Quote:python3 -m venv /path/to/new/virtual/environment

I only do simple things.

Do I need to run Python in a virtual environment?

My friend uses Windows, poor guy. Can he run Python in a virtual environment in Windows??


RE: Do I need to make a virtual environment? - ndc85430 - Sep-19-2020

You don't necessarily need a virtual environment, no. If you think you might want different versions of the same library for different projects, say, then I'd consider it. Of course you can create virtual environments on Windows.


RE: Do I need to make a virtual environment? - snippsat - Sep-19-2020

(Sep-19-2020, 08:43 AM)Pedroski55 Wrote: I only do simple things.

Do I need to run Python in a virtual environment?
No,but it can make a lot sense to use in some projects.
(Sep-19-2020, 08:43 AM)Pedroski55 Wrote: My friend uses Windows, poor guy. Can he run Python in a virtual environment in Windows??
Let's try Wink
# Make
C:\code>python -m venv my_env

# Cd in
C:\code>cd my_env

# Acivate 
C:\code\my_env>C:\code\my_env\Scripts\activate

# Test pip
(my_env) C:\code\my_env>pip -V
pip 19.2.3 from c:\code\my_env\lib\site-packages\pip (python 3.8)

# Install
(my_env) C:\code\my_env>pip install requests
Collecting requests .....
Successfully installed certifi-2020.6.20 chardet-3.0.4 idna-2.10 requests-2.24.0
 urllib3-1.25.10

(my_env) C:\code\my_env>pip list
Package    Version
---------- ---------
certifi    2020.6.20
chardet    3.0.4
idna       2.10
pip        19.2.3
requests   2.24.0
setuptools 41.2.0
urllib3    1.25.10

(my_env) C:\code\my_env> 
The main purpose of Python virtual environments is to create an isolated environment for Python projects.
This means that each project can have its own dependencies,regardless of what dependencies OS Python has or other project has.
As a example when doing web-development it make a lot of sense to use virtual environment,as one day may need to move code and dependencies to a server.


RE: Do I need to make a virtual environment? - ndc85430 - Sep-19-2020

You should also always use a requirements.txt file to declare the dependencies and their versions.


RE: Do I need to make a virtual environment? - deanhystad - Sep-19-2020

I would put it off until you are comfortable using Python. Virtual environments caused me much confusion when I was first starting. I would install a package on my computer and get an error when I import the package in my program. Now that I am a bit more savvy, I see the benefit virtual environments provide.


RE: Do I need to make a virtual environment? - seandepagnier - Sep-21-2020

if you need to ask the answer is probably no