Python Good Starting
When beginning your journey with Python, itβs crucial to set up your development environment properly to avoid potential conflicts and ensure a smooth experience. This guide will walk you through the steps of installing Python correctly using pyenv, managing different Python versions, and setting up a virtual environment for your projects.
Table of Contents
- Python Good Starting
- Why Learn Python Correctly from Scratch?
- Pyenv
- VirtualEnv
- Install Dependencies
- Project Structure Overview
Why Learn Python Correctly from Scratch?
- Consistency Across Environments: Setting up a consistent Python environment ensures that
your code runs the same way on different systems
. - Avoid System Conflicts: Using tools like
pyenv
andvirtual environments
prevents conflicts with system-installed Python versions and other projects. - Manage Dependencies: Virtual environments help you manage project-specific dependencies without affecting your
global Python installation
(useful for Linux users).
Pyenv
pyenv
is a tool that allows you to easily install and manage multiple versions of Python. It helps you avoid conflicts with the system Python and manage different versions for different projects.
1. To install pyenv
:
On macOS/Linux:
curl https://pyenv.run | bash
2. Configure Shell for pyenv
After running the command, add the following lines to your shell startup file (.bashrc
, .zshrc
, etc.):
# Pyenv configuration
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
This step is also important to avoid pip conflicts. Create an alias in .bashrc
, .zshrc
, etc., to ensure you are using pip from the correct pyenv installation:
# Configure your PIP as well
alias pip='python -m pip'
3. Restart Shell
Restart your shell or run source ~/.bashrc
(or the corresponding file) to apply the changes.
4. Check Available Python Versions:
To see the available Python versions that you can install with pyenv
, use:
pyenv install --list
5. Install a Python Version with pyenv
To install a specific version of Python, use:
# Replace <version> with the desired version number (e.g., 3.9.1).
pyenv install <version>
6. Check Installed Python Versions
To see which Python versions are installed on your system and identify the active version:
pyenv versions
# Example Output:
system # Python from your system
3.7.17
* 3.11.9 # (the * represents the active version)
3.12.0
7. Select a Python Version
pyenv global <python-version>
VirtualEnv
virtualenv
is a tool to create isolated Python environments, ensuring that dependencies for one project donβt interfere with those of another.
1. Install virtualenv
pip install virtualenv
2. Create a Virtual Environment with virtualenv
Select the directory where you will start your project (typically a new folder), then run:
# You don't need to specify the python version because pyenv is doing it when you
# choose the global version that you want to use on the previous steps. Of course
# if you want to change the python version, you have to do it on pyenv first before
# execute this command.
virtualenv --python=python <python-env-name>
# Recommended to use "env" as the environment name
3. Activate the Virtual Environment
source <python-env-name>/bin/activate
When the environment is active, its name appears at the beginning of your terminal prompt:
(<python-env-name>) user@machine:~/<project-directory>$
Install Dependencies
After creating and activating the virtual environment, itβs a good practice to create a requirements.txt
file listing the Python libraries required for the project:
# requirements.txt file
requests==2.25.1
selenium==4.4.1
bs4==0.0.2
klein_config==4.0.2
Install all dependencies at once from requirements.txt
:
pip install -r requirements.txt
Project Structure Overview
Hereβs an example of a simple, well-organized Python project structure:
my_project/
βββ app/ # Your application
βββ env/ # Virtual environment directory (exclude from version control)
βββ requirements.txt # List of project dependencies
βββ .gitignore # Files and directories to ignore in Git
βββ README.md # Project overview and documentation