0% found this document useful (0 votes)
13 views5 pages

Python Standard Libraries and Packages and Virtual Environments

Practical file on python

Uploaded by

tejasjoshi1032
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views5 pages

Python Standard Libraries and Packages and Virtual Environments

Practical file on python

Uploaded by

tejasjoshi1032
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Practical 4 : Introduction to Standard Library, Virtual

Environments and packages in Python.

The Python Standard Library


Python’s extensive standard library includes built-in modules (written in C) for system
functionality like file I/O, and Python modules offering standardized solutions. This library helps
ensure portability by providing platform-neutral APIs.
On Windows, Python installers typically include the full standard library and additional
components. On Unix-like systems, Python is usually distributed as packages, so optional
components may need to be installed separately using the system's packaging tools.
Beyond the standard library, the Python Package Index (PyPI) offers a vast collection of third-
party packages and frameworks.

Python Virtual Environment


Python virtual environments create a virtual installation of Python inside a project directory. Users
can then install and manage Python packages for each project. This allows users to be able to
install packages and modify their Python environment without fear of breaking packages installed
in other environments.

What is Virtual Environment in Python?


A Python virtual environment is:
• Considered as disposable.
• Used to contain a specific Python interpreter and software libraries and binaries which are
needed to support a project.
• Contained in a directory, conventionally either named venv or .venv in the project
directory.
• Not considered as movable or copyable.
When you install Python software on your computer, it is available for use from anywhere in the
filesystem. This is a system-wide installation.
While developing an application in Python, one or more libraries may be required to be installed
using the pip utility (e.g., pip3 install somelib). Moreover, an application (let us say App1) may
require a particular version of the library − say somelib 1.0. At the same time another Python
application (for example App2) may require newer version of same library say somelib 2.0. Hence
by installing a new version, the functionality of App1 may be compromised because of conflict
between two different versions of same library.
This conflict can be avoided by providing two isolated environments of Python in the same
machine. These are called virtual environment. A virtual environment is a separate directory
structure containing isolated installation having a local copy of Python interpreter, standard
library and other modules.
Using the global Python installation, more than one virtual environments are created, each having
different version of the same library, so that conflict is avoided.

Creation of Virtual Environments in Python using venv


The module used to create and manage virtual environments is called venv. venv will install the
Python version from which the command was run (as reported by the --version option). For
instance, executing the command with python3.12 will install version 3.12.
To create a virtual environment, decide upon a directory where you want to place it, and run
the venv module as a script with the directory path:
python -m venv tutorial-env
This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it
containing a copy of the Python interpreter and various supporting files.
A common directory location for a virtual environment is .venv. This name keeps the directory
typically hidden in your shell and thus out of the way while giving it a name that explains why
the directory exists. It also prevents clashing with .env environment variable definition files that
some tooling supports.
Once you’ve created a virtual environment, you may activate it.
On Windows, run:
tutorial-env\Scripts\activate
On Unix or MacOS, run:
source tutorial-env/bin/activate
(This script is written for the bash shell. If you use the csh or fish shells, there are
alternate activate.csh and activate.fish scripts you should use instead.) Activating the virtual
environment will change your shell’s prompt to show what virtual environment you’re using, and
modify the environment so that running python will get you that particular version and
installation of Python. For example:

To deactivate a virtual environment, type:


deactivate
into the terminal.

Managing Packages with pip


You can install, upgrade, and remove packages using a program called pip. By default pip will
install packages from the Python Package Index. You can browse the Python Package Index by
going to it in your web browser.
pip has a number of subcommands: “install”, “uninstall”, “freeze”, etc. (Consult the Installing
Python Modules guide for complete documentation for pip.)
You can install the latest version of a package by specifying a package’s name:

You can also install a specific version of a package by giving the package name followed
by == and the version number:

If you re-run this command, pip will notice that the requested version is already installed and do
nothing.
You can supply a different version number to get that version, or you can run python -
m pip install --upgrade to upgrade the package to the latest version:

python -m pip uninstall followed by one or more package names will remove the packages from
the virtual environment.
python -m pip show will display information about a particular package:

python -m pip list will display all of the packages installed in the virtual environment:

python -m pip freeze will produce a similar list of the installed packages, but the output uses the
format that python -m pip install expects. A common convention is to put this list in
a requirements.txt file:

The requirements.txt can then be committed to version control and shipped as part of an
application.
Users can then install all the necessary packages with install -r:

You might also like