Credence Analytics - Python : Virtual Environment + requirments.txt

Python projects must always be developed in isolated or virtual environment so that all our dependencies are locked to the project only.

 · 1 min read

Python : Virtual Environment + requirments.txt

Python projects must always be developed in isolated or virtual environment so that all our dependencies are locked to the project only.

requirements.txt in Python is package.json of NodeJS

It contains all the packages required for running the python project.

Lets see how we can create a virtual environment in Python and generate a requirements.txt file

Creating & Managing Virtual Environment

Install

virtualenv is a tool to create isolated environment in Python. Install this tool using apt-get

fire the following command on bash terminal

$ sudo apt-get install python3-venv

Initialize

traverse to the project folder and run the following command

$ virtualenv venv

Activate

to begin using the virtual environment, it needs to be activated using the following command

$ source venv/bin/activate

install any new modules without affecting system default Python or other virtual environments using pip/pip3

$ pip3 install numpy scipy matplotlib

These packages will be installed in the project folder only under venv folder

Now we can generate requirements.txt using following command

$ pip3 freeze --local > requirements.txt

Deactivate

once your work is completed you stop the virtual environment

$ deactivate

Delete

There might be a case where you want to remove the virtual environment. You can simple do this my deleting the venv folder.

if you are done working in the virtual environment , you can deactivate it


Add a comment
Ctrl+Enter to add comment

T
123 2 years ago

a

T
123 2 years ago

T
123 2 years ago

<SCript>

T
123 2 years ago

<<script>>

T
123 2 years ago

<script></script>

T
123 2 years ago

a

T
123 2 years ago

a