DEV Community

Kalpak Palekar
Kalpak Palekar

Posted on

Python Important

Generate python virtual environment for your project

To generate python virtual environment(venv) for your project, "cd" into your project folder and run the below command:

Syntax: python -m venv <env-name>

python -m venv myenv
Enter fullscreen mode Exit fullscreen mode

Whenever you are working in your project then you have to activate your virtual environment, so that your third-party python modules which you will be installing to use in your project will be maintained in this virtual environment. To activate your venv you have to run below command:

Syntax: source <env-name>/bin/activate

cd myproject
source myenv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Once you complete your work for the day and now you will need to deactivate your venv run below command:

Syntax: deactivate

deactivate
Enter fullscreen mode Exit fullscreen mode

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.)

Syntax: pip install requests==2.6.0
Syntax: pip list
Syntax: pip freeze > requirements.txt
Syntax: pip install -r requirements.txt

pip install requests==2.6.0
Enter fullscreen mode Exit fullscreen mode
pip list
Enter fullscreen mode Exit fullscreen mode
pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode
pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
sc0v0ne profile image
sc0v0ne

Good job !!!

Collapse
 
untada profile image
Unknownntada

Venv + Poetry is game changer for python project

Collapse
 
blockvoltcr7 profile image
blockvoltcr7

Nice