Thursday, 11 June 2026

Create virtual env and export

 In a GitHub Codespace (or any Linux-based terminal), you can create a Python virtual environment and export its installed packages like this:

1. Create a virtual environment

From your project directory:

python3 -m venv .venv

This creates a virtual environment in a folder named .venv.


2. Activate the virtual environment

source .venv/bin/activate

Your prompt should change to show something like:

(.venv) user@codespace:~/project$

3. Install packages

For example:

pip install flask requests pandas

Or install from an existing requirements file:

pip install -r requirements.txt

4. Export installed libraries to requirements.txt

Once all dependencies are installed:

pip freeze > requirements.txt

This generates a file such as:

flask==3.1.0
pandas==2.3.0
requests==2.32.3

5. Recreate the environment later

On another machine or Codespace:

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Useful verification commands

Check which Python and pip are being used:

which python
which pip

They should point inside .venv:

/workspaces/my-project/.venv/bin/python
/workspaces/my-project/.venv/bin/pip

Deactivate the virtual environment

When you're done:

deactivate

A common workflow in Codespaces is:

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt   # or install packages manually
pip freeze > requirements.txt

You may also want to add .venv/ to your .gitignore so the virtual environment itself isn't committed to Git:

.venv/

No comments:

Post a Comment

Hello

Create virtual env and export

 In a GitHub Codespace (or any Linux-based terminal), you can create a Python virtual environment and export its installed packages like thi...