Setting Up Virtual Environment in Python

The Python Virtual Environment System is a tool that functions to confirm that packages on different projects and, will be saved in different places as they won’t conflict.

However, the Python virtual environment will be kept in an isolated place from other projects as it has their dependencies for staying independent. This tutorial will tell you how to set the virtual Environment in Python.

How to Set a Local Environment

  • First, you have to open the terminal window and then type Python to check if the version is already installed.
  • If it is already installed, it is possible to get a message similar to that provided below:
$ python
Python 3.11.2 (main, Feb 8 2023, 14:49:24) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>>

How to install Python?

The recent source, binaries, and documentation will be available on the official website of Python https://www.python.org/.Python documentation can be downloaded from their website. Their documentation will be accessible in HTML, PDF, and PostScript formats.

Moreover, python distribution will be accessible on a wide range of platforms. Download only the binary code that applies to your platform and then install Python. But, if the binary code is not accessible on the platform, then use the C Compiler to compile the source manually. Through compiling the source code, it will provide flexibility depending on the features that are necessary for the installation.

How to install Python on Ubuntu Linux?

follow these steps to check if the python is already installed or not

  • Go and open the Linux terminal
  • Then, type the given below
$ python3.11 --version

Use the apt-Advanced Packaging Tool to install Python easily. Keep in mind to update the packages list in every configured repository.

$ sudo apt update

The Recent version won’t be there to install after doing the updation. To resolve this issue, add the dead snakes repository.

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa

Consider updating the package list once more.

$ sudo apt update

Type the command given below to install the Python 3.11 version in the terminal.

$ sudo apt-get install python3.11

Check if the Python is installed correctly by entering the command given below

$ python3
Python 3.11.2 (main, Feb 8 2023, 14:49:24) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> print ("Hello World")
Hello World

>>>

Install Python On Other Linux

The steps to install Python on a Unix or Linux machine are the follows:

  • Open a website and then click the download link. https://www.python.org/downloads/.
  • Then, go to the link for installing the zipped source code which is available on the Unix/ Linux.
  • The next step is to install and extract files
  • followed by editing the modules for customizing those options.

Then, enter the command given below:

$ run ./configure script
$ make
$ make install

Install the Python from the standard location which is /usr/local/bin and their libraries in /usr/local/lib/pythonXX . Here, XX is considered as the version of Python.

How to use the Yum command

Red Hat Enterprise Linux won’t install Python 3 . Instead will use the yum command on CentOS and other related variants. The command to install Python-3 RHEL 8 are the following

$ sudo yum install python3

How to install Python on Windows?

Python’s version 3.10 cannot be installed on Windows 7. It is suggested to install Python from the official installer. Another way is to install from the https://www.python.org/downloads/windows/

The embedded packages and installers for 32 and 64-bit architecture are provided below

embeddable_packages

Install the 64-bit Windows

(https://www.python.org/ftp/python/3.11.2/python-3.11.2-amd64.exe)

Click the downloaded file to start the installation

Image-9.jpg

Then, click the Install Now button and select the installation folder which has a shorter path. Then, tick the second check box for updating the path variable.

The next step is to accept defaults which are available on the installation wizard for the installation to be completed.

Image-10

Then, open the Window Command Prompt terminal for running the Python for running the python and to find out the installation success.

C:\Users\Acer>python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Python standard library consists of an executable module known as IDLE (Integrated Development and Learning Environment). Check it in the Windows start menu and then begin to launch.

idle

IDLE consists of a Python shell and a customizable multi-window texter which has several features like syntax highlighting, smart indent, and auto-completion. It is considered a cross-platform that will perform on Windows, MacOS, and Linux. Additionally, it consists of a debugger that is required for setting up the breakpoints, stepping, and viewing the global and local namespaces.

The steps to install Python on a Windows machine are the follows:

  • First, open the web browser and then go to https://www.python.org/downloads/.
  • Go to the link of the Windows installer  python-XYZ.msi file. Where XYZ is the version that is required to be installed.
  • The Windows system should support the Microsoft Installer 2.0 save the installer file to the local machine and run the installer to check if the machine will support MSI.
  • Run the installed file. It will open the Python install wizard and is easy. Accept the default settings and then need to wait till the installation is complete.

How to install Macintosh?

Macs will have Python already installed. But the dates won’t be correctly updated. Check http://www.python.org/download/mac/  for knowing the steps to install the current version with the tool to support the development of Mac. The older Mac Os was introduced in 2003.

How to set up Path?

Programs and other executable files will be available in several directories. Hence, the Operating system will give a search path that can list out the directories and so, the OS Searches will be available for executables.

Moreover, this path is stored in an environment variable which is named string that is operated by the operating system. The variable has information that is accessible to the command shell and other programs. The path variable is provided name as PATH in Unix or Path in Windows.

In Mac OS, the installer can modify the path details. To get the Python interpreter from a particular directory. It is necessary to add the Python directory to your path.

How To Set Up Path at Unix/Linux?

follow the steps to add the Python directory to the path for a particular session in Unix :

a. In the csh shell- enter setenv PATH “$PATH:/usr/local/bin/python” and then click enter.

b. In the bath shell (Linux)- Type export PATH=”$PATH:/usr/local/bin/python” and click Enter.

c. In the sh or ksh shell- Enter Path = “$PATH:/usr/local/bin/python” and click enter.

Remember to note that  /usr/local/bin/python is considered as the path of the python directory.

How to set a path at Windows?

For adding the Python directory in the path for a particular session in Windows-

At the command prompt – type path %path%;C:\Python and then click enter.

Note- C:\Python is referred to as the path of the Python directory.

What are the Python Environment Variables

The important variables that are recognized by Python are the following:

Sr.NoVariable and Description
1.PYTHONPATHL- It works similarly to PATH. This variable will ask the interpreter the location of module files imported into the program.It has the Python source library directory and those directories will have the Python source code. PYTHONPATH can be preset by the Python installer.
2.PYTHONPATHL- It works similarly to PATH. This variable will ask the interpreter the location of module files imported into the program. It has the Python source library directory and those directories will have the Python source code. PYTHONPATH can be preset by the Python installer.
3.PYTHONHOME- It is considered an alternative for the module search path. Thus, it is fixed in the python-startup for changing libraries easily.
4/PYTHONPATHL- It works similarly to PATH. This variable will ask the interpreter the location of module files imported into the program. It has the Python source library directory and those directories will have the Python source code. PYTHONPATH can be preset by the Python installer.

How to Run Python?

The 3 different ways to run Python are-

Interactive Python

To start Python from Unix, DOS or any other system will give a command-line interpreter.

Type the Python command line

$python # Unix/Linux
or
python% # Unix/Linux
or
C:> python # Windows/DOS

How to add the Script from the command line

Python script will run the command line through the interpreter of the application by entering the command below

$python script.py # Unix/Linux

or

python% script.py # Unix/Linux

or

C: >python script.py # Windows/DOS

Integrated Development Environment

It is possible to run Python from the Graphical User Interface (GUI) environment as well. Additionally, it has a GUI application on our system that works with Python.

  1. Unix: IDLE is the first IDE (Integrated Development Environment) for Python on Unix systems.
  2. Windows: PythonWin is the initial Windows interface for Python, providing a GUI-based IDE.
  3. Macintosh: Python on Mac comes with the IDLE IDE, available for download from the main website in MacBinary or BinHex’d files.
  4. If you’re having trouble setting up Python on your system, ask your system administrator for help. Ensure that Python is set up correctly.
  5. There’s also an online Python compiler/interpreter that allows you to edit and run code directly in your browser. Click the run button to execute the provided “Hello, World!” code.

Conclusion

To conclude, this article will allow us to learn the steps to set up a virtual environment in Python. How to Set up Python in Windows and Linux is also included.

Setting Up Virtual Environment in Python-FAQs

Q1.What is virtualenv in Python?

Ans. It is referred to as a tool to make a Python-isolated environment.

Q2. How do I activate venv?

Ans. Activating can be done through the command source venv/bin/activate

Q3. What is an example of a virtual environment?

Ans. Email, Chat, and web-based document-sharing applications are some of the examples of virtual environments.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment