Working with Python: A Guide to Installation and Set-Up

Starting with Python can feel like embarking on an exciting journey. Whether you’re a data scientist, a working professional, or someone curious about programming, Python offers a versatile and user-friendly platform to build, analyze, and explore.

 

Install and set up Python on your computer

Python: The Heart of Your Journey
Think of Python as your compass, guiding you through the world of programming. Python is free to use and is available for a wide variety of systems, including Windows, MacOS, and Linux.  First things first, to leverage the power of Python, you’ll need to download and install Python. Visit the official Python website to make sure you have the correct components to support installation.

A Python Editor (IDE): Your Workshop
An Integrated Development Environment (IDE) is like a workshop equipped with all the tools you need.

Examples of IDEs:

  • ‘PyCharm’: Ideal for professional development.
  • ‘Jupyter Notebook’: Perfect for data analysis and exploration.
  • ‘Visual Studio Code’: A versatile option for various programming tasks.

‘Anaconda’: A Treasure Trove for Data Scientists
‘Anaconda’  is like a treasure trove filled with tools and libraries essential for data science.

Why Use ‘Anaconda’?

  • Prepacked Libraries:Anaconda’  comes with many scientific libraries preinstalled.
  • Integrated Tools: Includes ‘Jupyter Notebooks’  for interactive coding and visualization.
  • Easy Installation: Visit the ‘Anaconda’ website and follow the instructions for your operating system.

Package Management: Unlocking Python’s Potential
Python packages (also known as libraries) are like ready-made building blocks. They are collections of modules (prewritten pieces of code) that you can use to create something new.

Managing Packages with ‘pip’ and ‘conda’:

‘pip’ (Pip Installs Packages): pip is your guide to the library, helping you find and utilize packages from Python Package Index (PyPI)–a vast library, hosting thousands of third-party modules for Python. You can install a package with pip install package-name

‘conda’: For ‘Anaconda’ users, conda is an alternative guide that knows both PyPI and the conda package manager. To install a package with conda, you can use conda install package-name

Virtual Environments: Creating Customized Workspaces
A virtual environment is like a separate room in your house, dedicated to a specific hobby. It’s an isolated space where you can install packages without them interfering with other projects or your system’s Python interpreter. Virtual environments allow you to manage packages at the project level. It’s like having separate kitchens for baking and grilling; different projects can require different versions of the same package without any conflict.

Creating a Virtual Environment:

‘venv’: Included in standard Python installations, you can create a virtual environment using python -m venv myenv

‘conda’: ‘Anaconda’  users can create a virtual environment using conda create --name myenv

By understanding package management and virtual environments, you’re equipping yourself with essential tools for Python development. These tools help you organize your work, avoid conflicts between projects, and easily access a wide variety of functionalities. They are like having a well-stocked and organized workshop, where everything you need is readily available, and nothing gets in the way of your creativity.

 

Install and set up Python with ‘Anaconda’ and ‘Jupyter Notebook’

Step 1: Download and Install ‘Anaconda’

  1. Visit ‘Anaconda’s’  official website at Anaconda Distribution.
  2. Click on the “Download” button.
  3. Choose the appropriate version for your operating system (Windows, MacOS, or Linux).
  4. Start the installation process by opening the downloaded file and following the instructions.

Step 2: Create and Activate a Conda Environment

  1. Open your terminal or command prompt. If you need a refresher on basic command-line operations, you can refer to this reference guide.
  2. Run conda create --name myenv. Replace myenv with the name of your virtual environment. Here is a quick reference guide for additional support.
  3. Activate the environment using conda activate myenv.

Step 3: Install Packages with ‘Conda’

  1. Open your terminal or command prompt.
  2. Make sure you’re in your virtual environment. (You can use the command conda info --envs to list all the available environments on your system. The active environment will have an asterisk (*) next to its name. If you are not in your desired environment, repeat Step 2.3.)
  3. Use conda install package-name. Replace package-name with the name of the package you wish to install.

Some common Python packages include:

      • Programming Language: Python and R
      • Data Processing: ‘Numpy’ and ‘Pandas’
      • Data Visualization: ‘Matplotlib’ and ‘Seaborn’
      • Data Modelling: ‘Scikit-learn’ and ‘Tensorflow’
      • IDE: ‘Spyder’ and ‘Jupyter Notebook’

Step 4: Launch Jupyter Notebook

  1. Open the ‘Anaconda Navigator,’  which is installed alongside ‘Anaconda.’ 
  2. Make sure you are in your virtual environment by choosing it from the environment dropdown in the ‘Anaconda Navigator.’
  3. Click on the ‘Jupyter Notebook’  icon.
  4. You can also launch ‘Jupyter Notebook’  from your terminal by typing jupyter notebook after you’ve activated your virtual environment.

Step 5: Start a New Notebook

  1. Click on “New” in the upper right corner and select “Python 3” to start a new notebook.
  2. Rename your notebook.

 

What are optional ways to install and set up Python on a local computer?

While this tutorial focuses on the use of ‘Anaconda,’  there are other options for setting up Python.

Direct Python Download
Instead of using ‘Anaconda,’  you can download Python directly from the official website. Be sure to check the box that says “Add Python to PATH” during the installation process.

Use other IDEs
‘Visual Studio Code’  and ‘PyCharm’  are also popular Python IDEs. You can download these applications from their official websites. Once installed, you can add the Python extension (for ‘Visual Studio Code’) or configure the Python interpreter (for ‘PyCharm’).

Use ‘venv’ for Virtual Environments
Instead of conda, you can use venv to create virtual environments. This can be done by running python3 -m venv /path/to/new/virtual/environment  in your terminal.

Use pip for Installing Packages
Instead of conda, you can use pip to install packages. This can be done by running pip install package-name in your terminal.

 

Tips When Installing and Setting Up Python

Always download the version of ‘Anaconda’ that matches your Python version or the one you intend to work with. If you’re learning Python 3 (which is recommended as Python 2 has reached its end of life), ensure you download ‘Anaconda’  for Python 3.

Regularly check for updates. ‘Anaconda’  frequently releases updates, so you need to ensure you have the latest packages and bug fixes. You can run conda update --all in the terminal or command line to update all packages within your ‘Anaconda’  distribution.

Always work inside a virtual environment. This helps isolate dependencies required by different projects. When starting a new project, you can create a new environment with conda create --name my_new_project_env python=3.8. This ensures that any libraries or packages you install won’t interfere with those in other environments.

Consider setting up a shortcut. If you frequently launch ‘Jupyter Notebooks,’  consider setting up a shortcut or alias to launch it quickly. On MacOS or Linux, you can add the line  alias jn='jupyter notebook' to your .bashrc or .zshrc file. (Instructions for Windows find here.) Afterward, you can launch ‘Jupyter Notebook’  by simply typing jn in the terminal.

Learn the most useful commands in ‘conda.’ ‘Anaconda’ (conda) has a plethora of useful commands. Familiarize yourself with these to leverage the full power of ‘Anaconda.’  Example: conda list shows the packages installed in the current environment, conda search package_name  checks for available versions of a package and conda env list shows all your environments.

 

Gotchas When Installing and Setting Up Python

Error: conda: command not found

  • Cause: The system’s path variable does not include the directory where ‘Anaconda’  is installed. This might happen if the installer wasn’t able to automatically add the ‘Anaconda’  directory to the system’s PATH or if you didn’t select the option during installation.
  • Fix: Add ‘Anaconda’s’  bin directory to the system’s path. On MacOS or Linux, you can do this by editing your .bashrc or .zshrc file (depending on your shell) to include: export PATH="/path/to/anaconda3/bin:$PATH"

Error: Error executing Jupyter command ‘notebook’: [Errno 2] No such file or directory

  • Cause: This can happen if Jupyter is not correctly installed or if there’s a problem with the associated environment.
  • Fix: Make sure you have activated your environment and installed Jupyter into the active environment using: conda install jupyter

Error: PackageNotFoundError when trying to install a package with conda

  • Cause: The package name might be misspelled, or the package might not be available in the default ‘Anaconda’  channels.
  • Fix: Double-check the package’s name. If the package is not available in the default channels, you can search for it on Anaconda Cloud or use pip to install it — open your terminal or command prompt and use pip install package_name, replacing package_name with the actual name of the package you want to install.

Error: The kernel appears to have died. It will restart automatically. in Jupyter Notebook

  • Cause: The Python process running the kernel might have crashed due to various reasons, such as running out of memory or executing buggy code.
  • Fix: Ensure your machine has enough resources (RAM/CPU). Review your code for any obvious issues. If the problem persists, try creating a new conda environment and installing Jupyter in it.

Error: Jupyter Notebook cannot be accessed via a browser

  • Cause: Sometimes ‘Jupyter’  might not open automatically or might be blocked by firewall settings.
  • Fix:  After starting ‘Jupyter Notebook’  in the terminal, you’ll see a URL printed. Copy this URL and paste it into your browser. Ensure that any firewall or security software allows traffic on the port Jupyter is using (usually 8888).

Related Tags: