Installing Python and Running Codeπ± Beginner
Setting up a Python environment involves installing the Python interpreter and mastering pip (the package installer) and Virtual Environments.
What is a Virtual Environment?
In Node.js, when you install packages, they go into a local node_modules folder inside your project. By default, Python does the opposite: it installs packages globally across your entire computer. A Virtual Environment (venv) is a tool that creates an isolated sandbox for a specific project so its libraries don't conflict with other projects.
Why are venvs Mandatory?
If Project A requires Django version 2, and Project B requires Django version 4, installing them globally will destroy one of the projects. Using a virtual environment guarantees that Project A and B have their own isolated, independent copies of Django.
How to Setup and Run
Open your terminal inside a new project folder:
# 1. Create a virtual environment named 'venv'
python -m venv venv
# 2. Activate the virtual environment (Mac/Linux)
source venv/bin/activate
# (On Windows Command Prompt: venv\Scripts\activate)
# 3. Install a third-party package safely into the sandbox
pip install requests
# 4. Save your dependencies to a file (equivalent to package.json)
pip freeze > requirements.txtvenv/ folder to GitHub. It contains thousands of massive system files. Always add venv/ to your .gitignore file, and commit only your requirements.txt file.