Python environments and package management
basics of Python environments, package managers
Understanding Python Environments
A Python environment is an isolated workspace where you can install packages and dependencies without interfering with other projects. Imagine it as a dedicated container for your project: it includes a specific Python version, all the packages you need (with their exact versions), and its own pip or conda installation. This separation ensures that changes in one project won’t break another, making your development process smoother and more reliable.
Using separate environments is essential for modern Python development. It helps you avoid package version conflicts, lets you test your code with different Python versions, and keeps your projects isolated and reproducible. Managing dependencies becomes much easier when each project has its own environment, and you can confidently experiment or upgrade packages without worrying about breaking other work.
Python Installation Types
Standalone Python
The most basic way to get started is by downloading Python directly from python.org. This gives you a minimal installation with the core language and built-in pip for package management. It’s a lightweight option, ideal if you want full control and only need the essentials.
Anaconda Distribution
For data science and scientific computing, many developers prefer the Anaconda distribution. Anaconda is a comprehensive platform that comes with the Python interpreter, both conda and pip package managers, and a wide range of pre-installed scientific libraries. It also includes tools like Anaconda Navigator (a graphical interface) and IDEs such as Spyder, making it a great choice for those who want a ready-to-use environment for analytics and research.
Package Managers: pip vs conda
Python’s ecosystem offers two main package managers: pip and conda. Pip is the default and works in any Python environment, installing packages from the Python Package Index (PyPI). Conda, on the other hand, is both a package and environment manager, capable of installing Python and non-Python packages, and is especially useful in the Anaconda ecosystem.
Common pip commands:
# Install a package
pip install package_name
# List installed packages
pip list
# Save package list
pip freeze > requirements.txt
# Install from requirements
pip install -r requirements.txt
Common conda commands:
conda create --name myenv python=3.9 # Create environment
conda activate myenv # Activate environment
conda install package_name # Install package
conda list # List packagesWhen should you use each? Pip is best for pure Python packages, getting the latest versions, or working outside the Anaconda ecosystem. Conda shines when you need data science tools, non-Python dependencies, or are already using Anaconda.
If you use Jupyter notebooks or some IDE consoles, you might see commands like !pip install .... The exclamation mark tells the notebook to run the command in the system shell, just as you would in a terminal. In regular terminal or command prompt sessions, you can simply use pip without the exclamation mark.
System Path and Environment Variables
The system PATH is a list of directories where your operating system looks for executable programs. It’s what allows you to run Python from any location in your terminal. If Python isn’t on your PATH, you’ll get errors when trying to run it from the command line.
Environment variables are system-wide settings that programs can access. For Python, the most important ones are:
PATH: Where the system looks for executablesPYTHONPATH: Where Python looks for modulesCONDA_PATH: Where conda environments are stored
Viewing/Setting Path Variables
On Windows, you can check and set your PATH like this:
echo %PATH%
setx PATH “%PATH%;C:\your\new\path”
On Linux or Mac:
echo $PATH
export PATH=$PATH:/your/new/pathCommon Commands and Troubleshooting
To check your installations, use:
python --version
pip --version
conda --version
To see your environment info in Python:
# In Python console
import sys
print(sys.path) # Show Python path
print(sys.executable) # Show Python locationCreate and Manage Virtual Environments
Python makes it easy to create isolated environments. With the built-in venv module, you can run:
python -m venv myenv
source myenv/bin/activate # Linux/Mac
myenv\Scripts\activate # Windows
Or, using conda:
conda create --name myenv python=3.9
conda activate myenv
conda deactivate

