Skip to content

Instantly share code, notes, and snippets.

@EstebanMqz
Last active May 6, 2024 21:12
Show Gist options
  • Save EstebanMqz/d42cef9a50e7110c4ede62cc8c251edb to your computer and use it in GitHub Desktop.
Save EstebanMqz/d42cef9a50e7110c4ede62cc8c251edb to your computer and use it in GitHub Desktop.
Environment Setup

Virtual Environments

pipenv pipenv

  • 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 conda

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

subprocess subprocess

At the end, the usage between them depends on the user's preference and software objectives.

Create environment :

  1. conda create --name env_name - Create environment with conda
  2. python3 -m venv env_path - Create environment with venv
  3. pipenv install - Create environment with pipenv
  4. direnv edit .envrc - Create environment with direnv

Activate environment :

  1. conda activate env_name - Activate environment with conda
  2. source env_path\bin\activate - Activate environment with venv
  3. pipenv shell - Activate environment with pipenv
  4. direnv allow - Activate environment with direnv

Requirements.txt (modules used):

#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)
Create file (.txt):
script.get_requirements(docstring)
Output contained (.txt) e.g:
# -- -------------------------------------------------------------------------------------- -- # 
# -- 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 
Install pkgs in requirements.txt:
%%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 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: subprocess script that allows other users to set up a python venv.
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: Resume LinkedInBusiness Gmail Github GitLab
Tags: pipreqs glob pip install import tabulate

Note: If the pkg installation is to reproduce a global environment.

See also:     Python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment