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:
pip install notebook
pip3 install notebook
python -m venv .venv
python3 -m venv .venv
.venv
folder for you virtual environment.venv\Scripts\activate
source .venv/bin/activate
(.venv)
at the start of each new line in the terminal.Select Kernel
in the top right of the notebook. Select Python Environments
and .venv
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