Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22464

Python Morsels: Using virtual environments in Python

$
0
0

Virtual environments can isolate the dependencies for your different projects. It's a best practice to make a new virtual environment (using the venv module) for each Python project you work on.

Python code with third-party requirements

Here we have a Python program called exponential.py that uses a module called rich:

fromargparseimportArgumentParserfromrich.consoleimportConsolefromrich.tableimportTableparser=ArgumentParser()parser.add_argument("start",type=float)parser.add_argument("annual_growth",type=float)parser.add_argument("--years",type=int,default=15)args=parser.parse_args()table=Table("Year","Old","Added","New")amount=args.startforyearinrange(1,args.years+1):new=amount*args.annual_growth/100table.add_row(f"{year}",f"{amount:,.0f}",f"{new:,.0f}",f"{amount+new:,.0f}",)amount+=newConsole().print(table)
fromargparseimportArgumentParserfromrich.consoleimportConsolefromrich.tableimportTableparser=ArgumentParser()parser.add_argument("start",type=float)parser.add_argument("annual_growth",type=float)parser.add_argument("--years",type=int,default=15)args=parser.parse_args()table=Table("Year","Old","Added","New")amount=args.startforyearinrange(1,args.years+1):new=amount*args.annual_growth/100table.add_row(f"{year}",f"{amount:,.0f}",f"{new:,.0f}",f"{amount+new:,.0f}",)amount+=newConsole().print(table)

Currently our exponential.py program doesn't work because we don't have that rich module:

~/exponential $ python3 exponential.py 10007
Traceback (most recent call last):
  File "/home/trey/exponential/exponential.py", line 2, in<module>
    from rich.console import Console
ModuleNotFoundError: No module named 'rich'
~/exponential $ python3 exponential.py 10007
Traceback (most recent call last):
  File "/home/trey/exponential/exponential.py", line 2, in<module>
    from rich.console import Console
ModuleNotFoundError: No module named 'rich'

The rich module that we're trying to use comes bundled in a third-party package called rich which, according to pip, we don't currently have installed:

~/exponential $ python3 -m pip list
Package            Version
------------------ ---------
argcomplete        2.0.0
certifi            2021.10.8
charset-normalizer 2.0.12
click              8.0.4
idna               3.3
packaging          21.3
pip                22.0.4
pipx               1.0.0
pyparsing          3.0.7
requests           2.27.1
setuptools         58.1.0
urllib3            1.26.9
userpath           1.8.0
~/exponential $ python3 -m pip list
Package            Version
------------------ ---------
argcomplete        2.0.0
certifi            2021.10.8
charset-normalizer 2.0.12
click              8.0.4
idna               3.3
packaging          21.3
pip                22.0.4
pipx               1.0.0
pyparsing          3.0.7
requests           2.27.1
setuptools         58.1.0
urllib3            1.26.9
userpath           1.8.0

Before we go about pip installing the rich module, we're going to create a virtual environment for this project.

The basics of virtual environments

Virtual environments are a way …

Read the full article: https://www.pythonmorsels.com/virtual-environments-in-python/


Viewing all articles
Browse latest Browse all 22464

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>