- Higher-level pkg manager for Python that combines pip pkg installations and virtualenv creation.
- Simpler & streamlined way to manage dependencies & venv for Python projects, with automatic environment creation & dependency resolution.
- Uses virtualenv, widely used tool for creating isolated Python environments with a not so user-friendly interface.
- Pipfile.lock file to ensure that your project's dependencies are correctly installed.
- pipenv uses
hashes
to verify the integrity of pkgs preventing malicious pkg installations. - pipenv integrates with other tools like pytest, flake8, and black, making it easier to use these tools in your project.
- Conda is a pkg for other languages with a cross-platform compatibility.
- Automatic environment creation with dependency-resolution.
- Pkg manager with its own environment management system.
- Earlier pkg releases are supported for testing & development.
- Built-in module that generates processes from terminals (cmd, bash, powershell) , connects to their input,output & error pipes while returning their respective output codes (0: success, 1: error, 2: warning).
- It can be used to create a venv with the arg. in a sequence of strings.
- It can be used to install, update or uninstall modules using pkg managers:
pip
,conda
,easy_install
,poetry
,pipenv
,virtualenv
,venv
,conda-forge
but it's used for gral. command running purposes. subprocess
is considered lower-level because of its manual config., but has more control over processes.
At the end, the usage between them depends on the user's preference and software objectives.
Create environment :
conda create --name env_name
- Create environment withconda
python3 -m venv env_path
- Create environment withvenv
pipenv install
- Create environment withpipenv
direnv edit .envrc
- Create environment withdirenv
Activate environment :
conda activate env_name
- Activate environment withconda
source env_path\bin\activate
- Activate environment withvenv
pipenv shell
- Activate environment withpipenv
direnv allow
- Activate environment withdirenv
#Pre-built modules
import subprocess, glob
def get_requirements(docstring, ext):
{"""
Function to create requirements.txt with required packages & versions to
setup a virtual environment for a project's execution.
Parameters:
----------
+ docstring: str
Docstring of requirements.txt script usually: (repository, requirements, author, license, environment).
+ ext: str
Extension of the scripts to extract modules used and versions for requirements.txt file creation/update.
Returns:
-------
+ requirements.txt: .txt file
.txt script with modules & versions used in repository to setup or update venv & ensure collaborations.
"""}
subprocess.run(["pipreqs", "--encoding", "utf-8", "./", "--force"])
with open("requirements.txt", "r+") as f:
old = f.read()
f.seek(0)
f.write((docstring + old).replace("==", " >= "))
f.write("jupyter >= 1.0.0 \n")
with open(glob.glob('*.txt')[0], 'r') as file:
lines = file.readlines()
for line in lines:
if '~' in line: #Typo error= ~ (<=) are invalid pkgs.
lines.remove(line)
with open(glob.glob('*.txt')[0], 'w') as file: file.writelines(lines)
with open(glob.glob('*.txt')[0], 'r') as file: print(file.read())
script = glob.glob(ext)
return print("scripts:", script)
script.get_requirements(docstring)
# -- -------------------------------------------------------------------------------------- -- #
# -- project: repo-name. -- #
# -- script: requirements.txt (File that containns pkgs in repository.) -- #
# -- author: user-name. -- #
# -- license: license-name. -- #
# -- environment: venv/requirements.txt. -- #
# -- -------------------------------------------------------------------------------------- -- #
fitter >= 1.2.3
matplotlib >= 3.5.3
numpy >= 1.25.0
pandas >= 1.4.4
plotly >= 5.6.0
scikit_learn >= 1.2.2
scipy >= 1.7.3
seaborn >= 0.11.2
tabulate >= 0.8.9
yahoofinancials >= 1.6
jupyter >= 1.0.0
ipython >= 8.10.0
%%capture
%pip install -r requirements.txt #1. Isolated: Separate installation from system/local in current kernel (virtual environment).
!pip install -r requirements.txt #2. Local: Installation of pkgs in global/system environment (local environment).
Deactivate Venv (if used):
If you are using a venv it should be deactivated to avoid pkg conflicts in other environments:
# Exit the current shell session
!exit
# Deactivate the current virtual environment
#!deactivate
# Exit the current shell session
#!pipenv shell exit
# Deactivate the current virtual environment
#subprocess.run(["deactivate"], shell=True)
Description | |
---|---|
Type: | Code Snippet ( .py ) |
Usage: | From its execution a requirements.txt file containing the pkgs and versions in the repo will be created by reading through repository's source files in cwd (scripts). The installation of the pkgs in the .txt file sets up a venv or gets to be made locally. |
Author: | |
Tags: |
Note: If the pkg installation is to reproduce a global environment.