Set up a Python Environment -Part1

Santosh Panigrahy
5 min readAug 6, 2023

--

Looking at the trend on most popular programming language since last few years and the recent trend of AIML, most of our fellow developers started following the trend. Though I have been using R programming so far, now I started learning python as part of my roadmap to career growth. Thus the first step towards that is setting up the python environment in such a way so that it would be easier to execute my python program in a smooth manner with strong basic knowledge of what and why.

[Note: I am assuming the installation of python in your system is already done. And we are ready to start python coding using your favorite IDE ( If you ask me, I am using vs code as of now.)]

Being from Java background, the first thing comes to my mind to learn any programming language is to understand the language itself and it starts from it’s rich library. And python has huge libraries that are wrapped up in the form of built-in modules or other packages that can be installed to use. So, let’s begin…

Python Package

Python package can be defined as a collection of python code bundled together to perform common tasks and can be reused using either built-in modules or third-party packages.

Any module that are not accessible through Python Standard Library are put together as third-party packages and are installed by PyPi, the Python Package Index. So, python packages are made available to public via Pypi, the python package index.

We use pip tool, a python package installer, to install, uninstall and upgrade python packages and internally it uses Pypi as the default source for its packages.

When you run a program and you get error like ModuleNotFoundError: No module named <module_name>, it indicates that that module is not available. And you need to install that module it uses that. Below is the snapshot, how the error looks like when I use requests module in my python program but it’s not installed in my machine.

Figure-1

We can install a package using pip using below syntax:

pip install <package_name>, where <package_name> is the name of the Python package that you want to install.

eg. pip install requests

Figure-2

You can use versioning as well while installing any package.

If you want to install a particular version of any package then you need to mention using == operator: For example, pip install requests==2.31.0 will, install version 2.31.0.

pip install requests~=2.1 will install the highest version available above 2.1, but not 3.0 or higher.

pip install requests~=2.2.0 will install the highest version available above 2.2.0, but not 2.3.0 or higher.

pip install requests>2.6.0 will install the highest version available above 2.6.0.

pip install “requests>2.5.0, <2.7.0” will install the highest version available above 2.5.0, but lower than 2.7.0

To see what are the packages installed already, use pip list or pip freeze

The difference between pip list and pip freeze is,

pip list lists all of the packages you have installed (and their dependencies) in a human-readable format.

pip freeze lists the packages you have installed (but not their dependencies) in a format suitable for storing in a file (often called a requirements.txt file)

Figure — 3.1 and 3.2

If you want to see more detailed information about a specific package installed, use pip show <package_name>

Eg. pip show matplotlib

Figure-4

You can pass multiple package names as well by separating packages with a space.

Figure-5

You can upgrade pip itself , if available for upgrade, by using — upgrade option as below:

python -m pip install — upgrade pip

Figure-6

Import Python Packages

Once you install required python module, you can use those in your program. And to use the module, we need to import it in our program.

import <package_name>

eg. import os

Figure-7

However, if you import like above, you need to use the package_name, eg. os above, we need to use it every time we call its function. Secondly, it will import the entire module even if you want to use only one functionality from the module, which is kind of overhead.

Figure-8

Alternatively, we can import only what functionality we want to use using from keyword along with import. Syntax: from <package> import *

For example, suppose you want to see your login user and your executable path values that are set in your environment, you can use getlogin() and get_exec_path() function from os module.

Figure-9
Figure-10

You can use alias as well if your package is a lengthy one and you don’t want to type a long string.

Figure-11
Figure-12

Hope, the above set up process will help anyone who wants to setup python and start the journey.

As the above is very basic setup, will continue virtual environment in part2 to make it short and meaningful :)

--

--

Santosh Panigrahy

Love to learn, share and grow in technologies to serve the world better :)