There are many ways to work with Python. Those learning Python for the first time often find it easiest to practice Python on the cloud using platforms like Google Colab or Dartmouth's JupyterHub. But, for researchers and students ready to build their own projects, it is often preferable to download and install Python on your own machine.
There are various different options for installing and working with Python locally on your own computer. Some common approaches include:
1. Installing the Anaconda distribution package for Python. This allows you to install hundreds of packages at once. For more on this option scroll down.
2. One folder - one project - one environment approach: It is good practice to create a separate folder on your computer for each project. However, it is also good practice to set up separate "virtual environments" for each project. This allows you to only install the packages necessary for a project and to, thus, more easily share your project with others. We provide our suggested method to follow this approach next:
While there are different options for installing and working with Python locally, the Dartmouth Library's Research Data Services team recommends the following option: working with Python within Visual Studio Code and setting up a unique virtual environment for each project. For more on what this means and how to do this, follow the instructions below:
Download and install Python following the instructions below.
Visual Studio Code (or VS Code) is a code editor developed by Microsoft and available on all major platforms (Linux, macOS, and Windows). It is entirely free and largely open-source. Essentially a text editor with superpowers, it supports not just Python but many other programming languages and is incredibly versatile thanks to a plethora of optional extensions. Since it can open, display, or run all kinds of different files, you rarely have to leave the program when working on a project, which makes it a great Integrated Development Environment (IDE).
Selecting a file with a single click opens it in a new tab, but closes it again if you switch to another file (note the name in italics on the tab)
Selecting a file with a double click opens it in a new tab and keeps it open until you close the tab (note the non-italicized name on the tab)
Installing Extensions in VS Code 1: Python extension
Open the Extensions view on the left-side pane. You can add additional functionality to VS Code by installing extensions.
To get started we will install the Python and Jupyter extensions.
Search for Python.
Click on "Install."
Close the "Welcome" tab that opens up
To check that it works, we will write some Python code to a file and execute it:
Open the test_code.py file you created above.
Type the following lines of code in the .py file:
print("Hello World!")
4 * 7
x = 26 / 4
print("x = ", x)
Run the code by clicking the ▶️ icon in the top right
A Python terminal should open in the bottom pane of VS Code. Examine what the output that was produced after running your code file. What information was returned? What was omitted?
Installing Extensions in VS Code 1: Jupyter extension(see fn. 1)
Python scripts always run from top to bottom in their entirety. This is great when you have a fully-formed program that you want to execute, but it is somewhat inconvenient when you want to have more of a back-and-forth, interactive experience. This is often the case when you are doing data analysis, create visualizations, or do any other kind of more iterative work.
To support such use cases, there is an extension for Python called Jupyter. We can install Jupyter on VS Code's `Extensions` tab:
Choose the `Extensions` tab on the Primary Side Bar
The `Jupyter` extension should be listed under the `Popular` heading, otherwise type `Jupyter` into the search box
Click on `Install`
[Explain more about importance of virtual environments]
ESC + B
or clicking on + Code
(to add a new code cell) or + Text
(to add a new text cell).Install
.import pandas as pd
ModuleNotFoundError
. This is because we need to install pandas into the local virtual environment before importing it. To do that we need to return to the terminal in VSC:(.venv)
at the start of each new line in the terminal.pip install [package-name]
pip3 install [package-name]
pip install pandas
pip3 install pandas
import pandas as pd
. It should work now.pip freeze
(for Windows) or pip3 freeze
(for Macs) in the terminal. Note: you will see far more than those packages you manually installed. This is because when you install a new package, pip automatically installs other packages that your requested package depends on.pip freeze > requirements.txt
(for Windows) or pip3 freeze > requirements.txt
(for Macs) into the terminal. You should see a new file called "requirements.txt" in the Explorer window on the left. Open the text file and review its contents. The requirements.txt will allow others to reproduce the exact same virtual environment on their computers and thus ensure they can run the same code as you.
pip freeze > requirements.txt
(for Windows) and pip3 freeze > requirements.txt
(for Macs) as needed before sharing the requirements.txt file with a collaborators.pip install -r requirements.txt
(for Windows) or pip3 install -r requirements.txt
(for Macs) into the terminal. This will install all packages (and their specific versions) listed in the .txt file.deactivate
.
Besides the above method, there are other ways to install Python on your own computer. Perhaps the most popular alternative is to install the Anaconda distribution package. To do so:
Teachers and students may choose to use Google Colab or Dartmouth's JupyterHub server. These are great options for instruction and training. However, neither resource allow you to save your work long-term, so are not recommended for research and project work.
For instructors and students using the JupyterHub, please see instructions below:
1. Open jhub.dartmouth.edu in a browser.
2. Choose "Reproducible Research Workshops"
3. Select "Start My Server." It may take a few minutes to load. Once open, your JHub page should look something like this (if you've opened it before, however, you may already have a notebook or other files open):
4. Use the screenshot below to find the root folder of your JHub directory:
5. You should then navigate to your workshop folder. For the Text Analysis in Python series, for example, you will want to go to the following directory: RR-workshops/text-analysis/text-analysis-with-python to find workshop materials. To get started with the Week 1 ("Strings and Files") workbook you will want to also open the strings-and-files folder
pip install notebook
pip3 install notebook
1. In the terminal type:
Windows: python -m venv .venv
Mac: python3 -m venv .venv