Python has a wide array of libraries with different versions and many dependencies that you can install on your computer to aid in your research. What do you do when you want to install a new library with dependencies that would conflict with another library you use? The answer is to define self-contained virtual environments. In this workshop, we will cover why and how to create virtual environments for your Python code development.
Prerequisites: Participants should be familiar with Python at the level of the Python Fundamentals Bootcamp, another introductory Python workshop, or be a self-taught Python coder.
Prior to the workshop, please install either Anaconda or miniforge (not both) and ensure that you are able to access the installed Python. Below are instructions on how to install either of these. How do you choose which to install?
Install Anaconda if:
- You are new to Python
- You are not comfortable working from the command line, and prefer point and click applications
- You want an easy 1-click installation solution
- You are OK with slower load times and some level of "bloatware"
- You will not use this installation of Python for commercial purposes while at Northwestern
Install miniforge and related software if:
- You are an experienced coder
- You are willing to work (minimally) from the command line
- You don't want any bloatware
- You would prefer to use open-source software
- You may use this Python installation for commercial research
If you are unsure which path to take, you should probably choose the first option and install Anaconda.
- Click here to go to the Anaconda download page.
- Provide your email address, agree to their terms, and download the installer.
- Run the installer on your computer.
- Open the Anaconda Navigator application on your computer (has a green circle icon).
- Within the Navigator, click on the Jupyter Notebook (or Jupyter Lab) icon.
- In the notebook that launches, create a code cell and type
1+1
and thenShift + Enter
(orShift + Return
) to exectute that cell. If it returns2
without any errors, then you have successfully installed Anaconda's Python, and you are ready to attend the workshop.
- Click here to go to the miniforge GitHub repo. Download the installer for your operating system, and run the installer on your computer. Accept all the defaults.
- I recommend that you install VS Code as your IDE. Click here to go to the VS Code website. Download the installer, and run the installer on your computer. Accept all the defaults.
- If you are on Windows, I recommend that you also install Git Bash (or work in WSL). Click here to go to the Git Bash website. Download the installer, and run the installer on your computer. Accept all the defaults. (If you are working on a Mac or Linux, you already have Bash installed.)
- Open VS Code, and set the default terminal by typing
Ctrl + Shift + p
then searching in the top bar forTerminal: Select Default Profile
. For Windows users, select Git Bash. Otherwise, select your default bash terminal for your OS. - Initialize
conda
(andmamba
) from the terminal as follows. TypeCtrl + ~
to open the terminal window at the bottom of VS Code. In the terminal type:(If you chose a non-standard installation path for miniforge, you may need to modify the command above to point to the proper file.) This will edit your./miniforge3/Scripts/mamba.exe init bash
.bash_profile
(or.bashrc
) file to initializeconda
andmamba
when you open a terminal. Close this terminal (e.g., typeexit
). Then every new terminal you open will have access toconda
,mamba
and the relatedpython
. - Within your terminal, create your first python virtual environment by typing:
(and confirming that you do want to install the packages.)
mamba create --name test-env python=3.12
- On the left panel of VS Code, click on the top icon for the
Explorer
. Then clickOpen Folder
. This should bring up a finder window allowing you to select (or create) a folder for code development. Select a folder. This should show all the files and sub-directories from that folder within the left side bar of VS Code. - Place your mouse within that left side bar to reveal the tools at the top that allow you to create new folders and files. (Alternatively, you can right-click in that side panel to open a menu.) Create a new file named
test.ipynb
. Then click on that new file to open it (if it doesn't open automatically) in the main window of VS Code. - Within the
test.ipynb
portion of the VS Code window, click onSelect Kernel
(in upper right). This may cause VS Code to ask you to install theJupyter
andPython
extensions; install those as requested. (You would only need to install these extensions once.) After those extensions are installed (and after clicking onSelect Kernel
), you should see the option at the top panel forPython Environments
. Click on that, and then select yourtest-env
. - Within your
test.ipynb
file, create a code cell and type1+1
and thenShift + Enter
(orShift + Return
) to exectute that cell. If it returns2
without any errors, then you have successfully installed Python andconda
/mamba
, and you are ready to attend the workshop.
If you have any trouble with the installation steps above, please use this link to submit a consultation request. Someone on our team will be happy to help you!