Setting Up Python with Conda

Before starting the coding tasks in this course, it is important to have a proper Python environment. Conda is a popular tool for managing Python versions, packages, and environments. Using Conda ensures that your code works consistently, avoids conflicts between packages, and makes it easier to reproduce results on any computer.

ℹ️ Note
You do not need to install Python separately if you are using Anaconda or Miniconda. Conda automatically manages Python installations inside each environment. Each environment can have its own Python version, completely isolated from the system Python.

Step 1: Install Conda

You have two main options:

  • Anaconda: Includes Python, Conda, Jupyter Notebook, and many scientific packages pre-installed.
    • Recommended if you want a ready-to-use setup for scientific computing.
    • Download Anaconda
  • Miniconda: Minimal installation with Python and Conda only.
    • Recommended if you want a smaller installation and prefer to install only the packages you need.
    • Download Miniconda

Step 2: Open a Conda-enabled terminal

  • Windows:
    • Use Anaconda Prompt (installed with Anaconda/Miniconda).
    • Alternatively, use PowerShell or Command Prompt if Conda was added to PATH.
  • macOS/Linux:
    • Open your regular Terminal app.
    • If this is the first time using Conda, initialize it by running:
      conda init
      

      Then restart the terminal.

ℹ️ Note
After opening the terminal, you should see (base) at the start of the prompt. This indicates the base Conda environment is active. Activating the correct environment ensures your code uses the right packages and versions.

Step 3: Create a new environment

It is highly recommended to create a separate environment for this course to avoid conflicts with other projects:

conda create -n npde_env python=3.10

where

  • npde_env is the name of the environment. You can choose any name you like.
  • python=3.10 ensures compatibility with the course exercises.
ℹ️ Note
You may choose a different Python version (e.g., python=3.11) or omit the version entirely. Newer versions may work, but some packages or examples could potentially be incompatible. Omitting the version installs the latest Python from your Conda channels, which may cause minor issues. Specifying python=3.10 ensures a consistent and reliable environment.

Step 4: Activate the environment

After creating your environment, you need to activate it before using it. To activate the environment:

conda activate npde_env

After running this command, your terminal prompt will change to something like:

(npde_env) C:\Users\YourName>

or on macOS/Linux:

(npde_env) your-computer:~$

The part in parentheses (npde_env) indicates that the npde_env environment is currently active.

⚠️ Warning
Always make sure the correct environment is active before running Python code for this course. If you forget to activate it, your code may use packages from the base environment, which can lead to errors or inconsistent results.

When you are done working, you can deactivate the environment:

conda deactivate

The terminal prompt will return to (base) or your default system prompt. Deactivating an environment does not delete it; it simply stops using it until you activate it again.

Step 5: Install required packages

Once your environment npde_env is active, you need to install the Python packages required for this course. These packages provide the tools to perform numerical computations, visualizations, and scientific calculations.

Option 1: Default Conda channel

conda install numpy matplotlib scipy

Option 2: Conda-Forge (recommended for latest versions)

conda install -c conda-forge numpy matplotlib scipy
⚠️ Warning
Make sure your environment npde_env is active when installing packages. Installing in the wrong environment (or in base) can cause version conflicts or unexpected behavior.

Step 6: Verify installation

After installing the required packages, verify that everything is working correctly. This helps catch any installation or configuration issues early.

With your npde_env environment active, open Python in the terminal:

python

On Windows (Anaconda/Miniconda Python), you may see:

Python 3.10.12 (tags/v3.10.12:aa12345, Jul  5 2025, 12:34:56) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

On Linux/macOS, you may see:

Python 3.10.12 (default, Jul  5 2025, 12:34:56)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

where

  • Python 3.10.12 indicates the Python version installed in your environment.
  • [MSC v.1934 64 bit (AMD64)] or [GCC ...] shows the compiler used to build Python.
  • >>> is the Python prompt, ready to accept commands.
ℹ️ Note
The exact version and build may vary slightly depending on your Conda installation, but as long as the prompt shows >>> and the correct Python version, your environment is ready.

At the Python prompt, type the following:

import numpy as np
import matplotlib.pyplot as plt
import scipy

print("All packages installed correctly!")

If no errors appear, the packages are installed correctly, and you are ready to run Python code in this environment. If an error occurs, it typically indicates a missing package or a version conflict, and you will need to check your environment and installation before proceeding.

When you are done, you can exit Python using any of the following methods:

  • Using a function:
    >>> exit()
    

    or

    >>> quit()
    
  • Using a keyboard shortcut:
    • Windows/Linux: Ctrl + Z then press Enter
    • macOS: Ctrl + D

After quitting, your terminal prompt will return to the Conda environment prompt, e.g.:

(npde_env) C:\Users\YourName>

or

(npde_env) your-computer:~$
⚠️ Warning
Make sure your npde_env environment is active when running Python. If you accidentally run Python in another environment (like base), your packages may not be available or may be different versions, leading to errors or inconsistent results.

Managing Conda Environments (Optional / Tips)

List all Conda environments

To see all Conda environments on your system and which one is active, run:

conda env list

or

conda info --envs

Example output:

# conda environments:
#
base                  *  C:\Users\YourName\Anaconda3
npde_env                 C:\Users\YourName\Anaconda3\envs\npde_env
other_env                C:\Users\YourName\Anaconda3\envs\other_env

where

  • * indicates the currently active environment.
  • base is the default Conda environment; avoid installing course packages here to prevent conflicts.

If you want to see more details about each environment, including Python version and environment paths, you can run:

conda info

This shows:

  • Active environment
  • Conda version
  • Python version in each environment
  • List of installed packages (briefly)
  • Environment locations on your system

Switch between environments

To switch from your current environment to another one, use:

conda activate <environment_name>

Example: Switch to the course environment:

conda activate npde_env

Your terminal prompt will change to indicate the active environment:

(npde_env) C:\Users\YourName>

or

(npde_env) your-computer:~$

To stop using the current environment and return to the base environment:

conda deactivate

You can chain multiple deactivate commands if needed; the prompt will eventually return to (base) or your system default.

Delete an environment

To remove an environment completely:

conda remove -n <environment_name> --all

Example: Delete the course environment (if you no longer need it):

conda remove -n npde_env --all

where

  • -n npde_env specifies the environment to delete.
  • --all removes all packages and the environment folder.

Conda will ask for confirmation before deleting. Type y to proceed. After deletion, the environment will no longer appear in:

conda env list