Set Up Python for Data Science

Set up Python for data science with this hands-on tutorial. Learn the essential tools, step-by-step setup, troubleshooting, and what to study next.

Focus: set up python for data science

Sponsored

You've decided to learn Python for data science — great choice. But before you can analyze your first dataset, you need a working environment. The biggest pain point for beginners isn't the code itself; it's the setup. Package conflicts, confusing terminology (Anaconda vs. pip vs. virtualenv), and a million tutorials that assume you already have everything installed. It's enough to make you quit before you even start. This lesson kills that friction. You'll leave with a clean, reproducible Python environment built specifically for data science — and a workflow you can rely on for every project that follows.

The problem this lesson solves

The most common way new data scientists set up Python is to just install the latest Python from python.org and then pip install pandas numpy matplotlib — and it often works on the first try. But then, a few weeks later, you install another package and suddenly scikit-learn won't import. Or you update your system Python and your Jupyter notebooks stop launching. You've just met the two biggest enemies of data science setup: dependency conflicts and environment chaos.

Data science relies on dozens of interdependent packages — NumPy, pandas, SciPy, matplotlib, and many more, each with its own version requirements. Without an isolated environment, one upgrade can silently break your entire workflow. This lesson solves that problem by establishing a clean, project-based setup from day one. You'll not only get Python running with the essential data stack, you'll build a process that makes your work reproducible — for yourself, your team, or your future self.

By the end of this lesson, you will be able to:

  • Explain the core idea behind Python environment management for data science
  • Create and activate a dedicated virtual environment for a data science project
  • Install NumPy, pandas, matplotlib, and Jupyter inside that environment
  • Launch Jupyter Notebook or Jupyter Lab and verify everything works together

Core concept / mental model

Think of a Python virtual environment as a sandbox — a separate, self-contained folder on your computer that holds its own Python interpreter and its own set of installed packages. When you activate it, your shell uses the Python and libraries from that sandbox, not from your system.

Analogy: Imagine your computer as a shared kitchen. If everyone used the same utensils and ingredients, recipes would collide — someone leaves you with no salt, or a pan you need is dirty. A virtual environment is like booking your own private cooking station: all the tools you need are right there, and you don't disturb anyone else. When you're done, you clean up and leave no trace.

Why this is especially important for data science:

  • Reproducibility: You can freeze the exact versions of every package (pip freeze > requirements.txt). A teammate or a future you can recreate the same environment with one command.
  • Isolation: A project that needs pandas 1.5 can live side-by-side with a project that needs pandas 2.1 — no conflict.
  • Sanity: You avoid the dreaded "it works on my machine" because the environment is tied to the project, not to your entire OS.

For data science, the standard stack is:

  • NumPy — the foundation for multidimensional arrays and math
  • pandas — DataFrames and Series for tabular data
  • matplotlib — the classic plotting library
  • Jupyter — the interactive notebook environment where you'll write and run your analysis

How it works step by step

The setup process can be broken into four logical steps. You'll walk through them in the hands-on section, but first let's understand the flow.

  1. Install Python (if you don't have 3.10+). You can use the official installer from python.org, or a version manager like pyenv on macOS/Linux.
  2. Create a project folder for your new data science project. This keeps your files organized.
  3. Create a virtual environment inside that folder. Python's built-in venv module is the simplest and most standard way.
  4. Activate the environment and install the data science packages with pip. Everything stays inside the environment.
  5. Launch Jupyter from within the environment to start coding.

Here's the key mental chain:

  • python -m venv .venv creates the sandbox.
  • Activating it (source .venv/bin/activate on macOS/Linux, .venv\Scripts\activate on Windows) declares, "I'm working inside this sandbox now."
  • pip install places packages into the sandbox.
  • When you're done, deactivate leaves the sandbox, and your system Python is untouched.

This approach is lightweight and standard — no extra tooling needed. You can always upgrade to more advanced tools like conda or poetry later, but venv is the foundation every data scientist should understand.

Hands-on walkthrough

Let's build a complete, working data science environment. I'll assume you have Python 3.10+ installed; if not, go to python.org/downloads and install it first. You'll also need to open a terminal or command prompt.

Step 1: Create your project folder and virtual environment

Open your terminal and run:

mkdir mydata
cd mydata
python -m venv .venv
  • mkdir mydata creates a new directory for the project.
  • python -m venv .venv creates a virtual environment named .venv inside the project folder. You'll see a hidden folder with that name — that's your sandbox.

Step 2: Activate the environment

Activation changes your shell's PATH so that python and pip point to the environment, not the system.

  • macOS / Linux: source .venv/bin/activate
  • Windows (Command Prompt): .venv\Scripts\activate
  • Windows (PowerShell): .venv\Scripts\Activate.ps1

After activating, your prompt should show (.venv) at the beginning, confirming the environment is active.

(.venv) $ python --version
Python 3.11.3

Pro tip: Always run python --version and which python after activating to confirm you're using the right interpreter. It's a quick sanity check that saves hours of confusion.

Step 3: Install the data science stack

Now install the core packages:

pip install numpy pandas matplotlib jupyter

This installs the latest versions of NumPy, pandas, matplotlib, and Jupyter into your virtual environment. The packages come with their own dependencies, which pip resolves automatically.

Step 4: Verify the setup

python -c "import numpy, pandas, matplotlib; print('Stack ready:', numpy.__version__, pandas.__version__, matplotlib.__version__)"

You should see output like:

Stack ready: 1.24.3 2.0.3 3.7.1

Your exact versions may differ, but as long as the import succeeds, you're good.

Step 5: Launch Jupyter and test a quick computation

jupyter notebook

Your browser will open the Jupyter dashboard. Create a new Python notebook and run the following in a cell:

import numpy as np
import pandas as pd

# Create a simple DataFrame
df = pd.DataFrame({
    "feature": [1, 2, 3, 4],
    "value": np.array([10, 20, 30, 40]) * 2
})

df.describe()

You should see a table with summary statistics. That's your environment working end-to-end. 🎉

Compare options / when to choose what

You now know the venv + pip approach. But there are other popular ways to set up Python for data science. Here's a comparison to help you choose:

Option Pros Cons Best for
venv + pip (this lesson) Lightweight, built-in, minimal learning curve, standard Manual management of dependencies; you handle versions yourself Most data science projects, beginners, teams needing simple reproducibility
Anaconda / conda Pre-installed data stack, handles non-Python dependencies (like C libraries), great for Windows users Heavy (hundreds of packages), slower package solving, can conflict with system Python Beginners who want everything pre-installed, Windows users, projects with complex native dependencies
Poetry Declarative pyproject.toml, lockfile for exact versions, handles both dependencies and project packaging Slightly more complex to learn, still uses pip under the hood Python projects that are also packages, teams wanting strict reproducibility
Docker Full environment including OS, perfect for deployment, reproducible anywhere Steep learning curve, resource-intensive, overkill for local learning Production deployments, complex multi-service projects

When to choose what:

  • Stick with venv + pip for most learning and small-to-medium projects. It's the most transparent and lightweight.
  • Consider Anaconda if you're on Windows and want to avoid any native dependency headaches (e.g., installing a package that needs a compiler). It's also great for complete beginners who want a single installer.
  • Graduate to Poetry or Docker when you need to share your environment with a team or deploy to the cloud.

Pro tip: Whatever you choose, always keep a requirements.txt file in your project. After installing packages, run pip freeze > requirements.txt to record every package and version. This makes your environment reproducible in one command: pip install -r requirements.txt.

Troubleshooting & edge cases

Even with a clean setup, things go wrong. Here are the most common issues and fixes:

python is not recognized / command not found

  • Cause: Python isn't in your system PATH.
  • Fix: Reinstall Python and check "Add Python to PATH" during installation. On macOS, use python3 instead of python if the default python points to Python 2.

Activation script fails (PowerShell on Windows)

.venv\Scripts\Activate.ps1
  • Error: "Cannot be loaded because running scripts is disabled on this system."
  • Fix: Open PowerShell as admin and run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned (you can read more about this in Microsoft's docs). Or use Command Prompt instead, which doesn't have this restriction.

pip installs packages to the wrong location

  • Symptom: You import a package and get a ModuleNotFoundError, even though you installed it.
  • Cause: You weren't in the activated environment when you ran pip install.
  • Fix: Activate the environment first, then run which python (or where python on Windows) to confirm it shows your .venv path. Reinstall the package.

Jupyter runs but doesn't see your environment's packages

  • Symptom: You run jupyter notebook from inside your environment, but the notebook can't import pandas.
  • Cause: Jupyter is launching a different kernel.
  • Fix (quick): Instead of jupyter notebook, you can start Jupyter with python -m notebook — this explicitly uses the Python from your environment. For a permanent solution, install ipykernel and create a kernel spec: pip install ipykernel then python -m ipykernel install --user --name=mydata.

Version conflicts (e.g., pandas requires numpy>=1.20 but you have 1.19)

  • Fix: Update numpy explicitly: pip install --upgrade numpy. If that doesn't solve it, you may have multiple environments confused. Create a fresh virtual environment to start clean.

"Python is already installed, but I wanted the conda version"

  • If you already have system Python and another environment manager, you can mix them, but it's easy to get tangled. A safe approach is to always use virtual environments and never install packages globally with pip.

What you learned & what's next

Congratulations — you've completed the foundational setup. Let's recap what you accomplished:

  • You explained the core idea behind Python environment management: isolation and reproducibility via virtual environments.
  • You created and activated a .venv virtual environment.
  • You installed the essential data science stack: NumPy, pandas, matplotlib, and Jupyter.
  • You verified your setup by launching Jupyter and running a small pandas computation.
  • You compared different setup approaches (venv, Anaconda, Poetry, Docker) and learned when to pick each.
  • You now know how to troubleshoot the most common setup pitfalls.

This environment is your launchpad for the rest of the Python for data science track. Everything you'll learn next — from NumPy arrays to pandas DataFrames and visualization — will run inside this sandbox.

What's next? The natural next lesson is Understanding NumPy Arrays, where you'll dive into the core data structure of scientific computing. You'll use your freshly set up environment to run your first NumPy operations. Ready? Let's keep going.

Practice recap

Create a new project folder, set up a virtual environment, and install the data science stack. Then run pip freeze > requirements.txt and examine the file. Finally, launch Jupyter Notebook and create a notebook that imports pandas and prints the version — a quick check that your environment is ready for the next lesson.

Common mistakes

  • Installing packages globally without a virtual environment, causing version conflicts across projects.
  • Forgetting to activate the virtual environment before running pip install, so packages go to the system Python.
  • Using python instead of python3 on macOS/Linux, which may invoke an older Python version.
  • Not pinning package versions with pip freeze > requirements.txt, making the environment irreproducible.

Variations

  1. Use Anaconda or Miniconda to get a pre-installed data science stack and conda's environment manager.
  2. Use the pyenv tool to manage multiple Python versions on macOS/Linux before creating virtual environments.
  3. Use Poetry or Pipenv for more advanced dependency management with a lockfile.

Real-world use cases

  • A data analyst sets up a fresh environment per client project to prevent dependency conflicts and ensure reproducible analyses.
  • A machine learning engineer packages their model with a requirements.txt file so teammates can recreate the environment in staging and production.
  • A university instructor provides a single requirements.txt on the first day of class so every student has the same data science stack.

Key takeaways

  • A virtual environment isolates your Python packages, preventing dependency conflicts between data science projects.
  • Using Python's built-in venv module is the simplest and most standard way to set up an isolated environment.
  • The essential data science stack — NumPy, pandas, matplotlib, and Jupyter — installs with a single pip install command.
  • Always activate your environment before installing or running Python commands to ensure you're using the right interpreter.
  • Use pip freeze > requirements.txt to make your environment reproducible for yourself and others.
  • Choose the right setup tool — venv for simplicity, Anaconda for ease, Docker for deployment.

Sponsored

Sponsored

Discussion

Questions, corrections, and tips help everyone reading this page.

0 comments

Add a comment

Shown publicly with your comment.

Be constructive · max 4,000 characters

No comments yet — start the thread.

Related tutorials, quizzes, and articles for this topic.