Skip to main content

Automatic Python environment manager for students and developers

Project description

๐Ÿ›ก safeenv

Automatic Python environment manager for students and developers

PyPI Python License CI


What is safeenv?

safeenv is a beginner-friendly Python CLI tool that automatically sets up, fixes, and diagnoses Python project environments.

Students and new developers often struggle with:

  • Creating and activating virtual environments
  • Generating requirements.txt files
  • Getting a cloned project to run
  • Mysterious ModuleNotFoundError messages
  • Missing or broken dependencies

safeenv makes these problems disappear with simple, memorable commands.


Features

Command What it does
safeenv init Create a .venv virtual environment in the current directory
safeenv freeze Scan all .py files and generate requirements.txt
safeenv setup Create .venv + install all dependencies (great after cloning)
safeenv doctor Diagnose missing venv, packages, or bad Python versions
safeenv fix Repair the environment automatically

Installation

pip install safeenv

Or install in editable/development mode from source:

git clone https://github.com/safeenv/safeenv.git
cd safeenv
pip install -e ".[dev]"

After installation, the safeenv command will be available globally.


Quick Start

1. Start a new project

mkdir my-project && cd my-project
safeenv init
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
  SafeEnv Initialization
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

  โœ“  Python version detected: 3.11
  โœ“  Virtual environment created: .venv

  Your environment is ready.

  To activate:

    Mac / Linux:
      source .venv/bin/activate

    Windows:
      .venv\Scripts\activate

2. Scan your project and generate requirements.txt

safeenv freeze
  Scanning project for imports...

  โœ“  Flask
  โœ“  numpy
  โœ“  pandas
  โœ“  requests

  โœ“  requirements.txt generated with 4 packages.

safeenv freeze uses Python AST parsing โ€” it reads your source files without executing them, so it is completely safe.

3. Get a cloned project running in one command

git clone https://github.com/someone/awesome-project.git
cd awesome-project
safeenv setup
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
  Project Setup
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

  Checking environment...

  โœ“  Python version compatible: 3.11
  โœ“  Virtual environment created: .venv

  Installing dependencies...

  โœ“  Flask installed
  โœ“  numpy installed
  โœ“  pandas installed

  Project setup complete.

4. Check project health

safeenv doctor
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
  Project Health Report
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

  Python version      :  3.11
  Virtual environment :  detected
  requirements.txt    :  found

  Issues found:

  โš   Missing dependency: torch
  โš   Missing dependency: fastapi

  Recommendation:

    Run:  safeenv fix

5. Fix problems automatically

safeenv fix
  Repairing environment...

  โœ“  Virtual environment: OK
  
  Missing package detected: torch
  Installing torch...
  โœ“  torch installed

  Missing package detected: fastapi
  Installing fastapi...
  โœ“  fastapi installed

  โœ“  Environment repaired successfully.

Command Reference

safeenv init

Creates a .venv virtual environment in the current directory using the active Python interpreter.

safeenv init                    # current directory
safeenv init --dir /path/to/project
  • Safe to run multiple times โ€” will never overwrite an existing .venv
  • Displays platform-specific activation instructions

safeenv freeze

Scans all .py files in the project using Python AST parsing and writes a requirements.txt.

safeenv freeze                           # writes requirements.txt
safeenv freeze --output deps.txt         # custom output file
safeenv freeze --dir /path/to/project    # specific directory

How it works:

  1. Recursively walks all .py files (ignores .venv, __pycache__, etc.)
  2. Parses each file with ast.parse() โ€” no code is executed
  3. Collects all import and from ... import statements
  4. Filters out Python standard-library modules
  5. Maps import names to correct PyPI names (e.g. cv2 โ†’ opencv-python)
  6. Writes a sorted requirements.txt

safeenv setup

The single command to run after cloning a project. Combines init + dependency installation.

safeenv setup
safeenv setup --dir /path/to/project

Steps performed:

  1. Checks Python version compatibility
  2. Creates .venv if not present
  3. Reads requirements.txt
  4. Installs each package into .venv

safeenv doctor

Performs a read-only health check and lists all issues. Never modifies anything.

safeenv doctor
safeenv doctor --dir /path/to/project

Checks:

  • Python version (minimum: 3.7)
  • .venv directory presence
  • requirements.txt presence
  • Packages listed in requirements.txt that are not installed

safeenv fix

Automatically resolves issues reported by safeenv doctor.

safeenv fix
safeenv fix --dir /path/to/project

Actions:

  • Creates .venv if missing
  • Installs any missing packages from requirements.txt

Import Name Mapping

safeenv freeze automatically translates common import names to their correct PyPI package names:

Import name PyPI package
cv2 opencv-python
PIL Pillow
sklearn scikit-learn
bs4 beautifulsoup4
yaml PyYAML
dotenv python-dotenv
dateutil python-dateutil
serial pyserial
jwt PyJWT
git GitPython

(and many more โ€” see dependency_scanner.py for the full list)


Requirements

  • Python 3.8 or newer
  • Works on Windows, macOS, and Linux
  • Runtime dependencies: typer and rich (both installed automatically)

Development Setup

git clone https://github.com/safeenv/safeenv.git
cd safeenv
pip install -e ".[dev]"

Run tests

pytest

Run tests with coverage

pytest --cov=safeenv --cov-report=term-missing

Project Structure

safeenv/
โ”‚
โ”œโ”€โ”€ safeenv/
โ”‚   โ”œโ”€โ”€ __init__.py          # Package metadata and version
โ”‚   โ”œโ”€โ”€ cli.py               # Typer CLI commands (init, freeze, setup, doctor, fix)
โ”‚   โ”œโ”€โ”€ env_manager.py       # Virtual environment creation and detection
โ”‚   โ”œโ”€โ”€ dependency_scanner.py # AST-based import scanning and requirements generation
โ”‚   โ”œโ”€โ”€ installer.py         # pip-based package installation helpers
โ”‚   โ”œโ”€โ”€ doctor.py            # Project health diagnostics
โ”‚   โ””โ”€โ”€ utils.py             # Shared Rich console and styled print utilities
โ”‚
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ conftest.py          # Shared pytest fixtures
โ”‚   โ”œโ”€โ”€ test_env_manager.py  # Tests for virtual environment logic
โ”‚   โ”œโ”€โ”€ test_dependency_scanner.py  # Tests for AST scanning
โ”‚   โ””โ”€โ”€ test_cli.py          # Integration tests for CLI commands
โ”‚
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ LICENSE
โ””โ”€โ”€ pyproject.toml

Contributing

Contributions are welcome! Here is how to get started:

  1. Fork the repository on GitHub
  2. Clone your fork locally:
    git clone https://github.com/your-username/safeenv.git
    cd safeenv
    pip install -e ".[dev]"
    
  3. Create a branch for your change:
    git checkout -b feature/my-improvement
    
  4. Make your changes โ€” follow the existing code style
  5. Run the tests to make sure everything still works:
    pytest
    
  6. Open a Pull Request with a clear description of what you changed and why

Guidelines

  • Keep the tool lightweight and beginner-friendly
  • Add tests for any new functionality
  • Write clear docstrings for new functions
  • Use type hints throughout
  • Prefer simple, readable code over clever one-liners

License

This project is licensed under the MIT License โ€” see the LICENSE file for details.


Built with โค๏ธ for students and developers who just want their code to run.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safeenv_tool-0.1.0.tar.gz (24.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safeenv_tool-0.1.0-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file safeenv_tool-0.1.0.tar.gz.

File metadata

  • Download URL: safeenv_tool-0.1.0.tar.gz
  • Upload date:
  • Size: 24.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for safeenv_tool-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5d000e5dbf1c94c0b9c9571474cf2896d3f7bf455c9b99086d7f7d8c2b94dadd
MD5 1fb3d4000a3936770fd0c7d071b445c9
BLAKE2b-256 a82de07cddeb20380d5255af3894b7fe4dca5c578177c0c490f79ccbb7f79eff

See more details on using hashes here.

File details

Details for the file safeenv_tool-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: safeenv_tool-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for safeenv_tool-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24d403ef0a90c4747fd543202d5903b28e3d262270fef5fc22ef74c229ef40b5
MD5 e34da8427f73c32360b99ce8b8ed3b62
BLAKE2b-256 c9dad87c1acbac8591e71a815b85f3740a643822f9494e349a9a7d73a862bb5f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page