Coder Social home page Coder Social logo

jmcriv / datafun-00-python-virtual-env Goto Github PK

View Code? Open in Web Editor NEW

This project forked from denisecase/datafun-00-python-virtual-env

0.0 0.0 0.0 173 KB

A short guide to setting up a local (or reusable) Python virtual environment to hold all the packages your project needs

datafun-00-python-virtual-env's Introduction

Local Python Virtual Environments using venv

A virtual environment is an isolated Python environment that allows you to manage project-specific dependencies without interfering with your system's global Python installation.

This guide:

  • Uses the built-in venv module
  • Creates a new subfolder in your project repository folder
  • Names the new virtual environment folder .venv (pronounced as "dot venv")

Explore the .venv folder as you create, activate, and install additional Python packages into that folder.

To learn more about why this process (over many other possibilities), see WHY.md. Join the discussion at discussions.

Terminals

We type these commands in a terminal. PowerShell Core works well on all operating systems. You may also use Terminal (Mac/Linux) or Windows PowerShell or Command Prompt (Windows). The commands have been tested in PowerShell Core. Copy this repo and update as needed for your process.

Step 1: Create the Virtual Environment

Open your terminal and navigate to your project directory. Create a new local virtual environment named .venv by typing the following command and hitting Enter (or Return):

python -m venv .venv

Verify the sub folder .venv is created. You can replace .venv with any name you prefer. We like the leading dot because it keeps environment files away from project code.

Step 2: Activate the Virtual Environment

To use the virtual environment, you need to activate it using a command found in the new virtual environment. The activation process differs based on your operating system.

On Windows:

.venv\Scripts\activate

On macOS and Linux:

source .venv/bin/activate

Verify your terminal prompt changes, indicating that you are now working within your virtual environment.

Step 3: Install Project Dependencies

With the virtual environment active, you can install the required packages for your project. What you install is specific to your project.

You can:

  • Install and upgrade multiple packages in a single line
  • Install packages one at a time
  • Install all packages listed in a requirements.txt file using the -r flag

Examples are shown below.

Verify your virtual environment appears in your terminal prompt before running installation commands. Let each command finish before running another.

python -m pip install --upgrade pip wheel
python -m pip install requests
python -m pip install pandas
python -m pip install -r requirements.txt

Optional/As Needed: Creating A New Requirements File

If you want to share your project with others or deploy it on a different system, it's a good practice to include the virtual environment details. This ensures that anyone working on your project has the same environment and dependencies.

To create a new requirements.txt with the packages in the virtual environment, run the following command:

pip freeze > requirements.txt

Optional/As Needed: Deactivate the Virtual Environment

deactivate

Summary: Activating a New Local Virtual Environment in PowerShell Core

These two commands create and activate a new virtual environment in PowerShell Core.

python -m venv .venv
.venv\Scripts\activate

Recommendations

  1. Practice creating, activating, deactivating virtual environments.
  2. Practice installing, uninstalling, and freezing requirements.
  3. Try different approaches. When a course uses pip, try pip. When a course uses conda, try conda.
  4. Be adaptable. Different teams do things differently, stay open to quickly learning new tools and methods.

Links

Screen Shots

Screenshot: Creating a Virtual Environment

datafun-00-python-virtual-env's People

Contributors

denisecase avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.