Note: these instructions are for a mac/linux environment. On Windows, managing python from the command line is difficult, so I recommend using Anaconda, which comes pre-installed with most scientific packages. Anaconda uses its own environment and package management system, so if you use Anaconda, do not follow any instructions here.
- Use a separate virtual environment for each project. This promotes reproducibility since your projects will be using consistent versions of packages.
- Keep virtual environments in a single location in your home directory (e.g.
~/venvs/
). This makes it easy to find them later on, and doesn't encumber your project directory with thousands of files and large python packages which will be annoying when you want to move/copy your project. - Don't put virtual environments in shared/sync folders (e.g. Dropbox) that will be used by multiple computers with different operating systems. This is because many packages install platform-specific binaries that won't run on other operating systems.
- Navigate to the directory where you store your virtual environments
[user@local ~]$ cd ~/venvs
- Create a virtual environment called
project-name
This creates a python virtual environment in[user@local venvs]$ python3 -m venv project-name
~/venvs/project-name
- Before you can run code or install packages, you will need to activate the virtual environment
You should now see a prefix in front of your prompt
[user@local ~]$ source ~/venvs/project-name/bin/activate
(project-name) [user@local ~]$
If you want to switch between environments, you must first deactivate your current active environment
(project-name) [user@local ~]$ deactivate
[user@local ~]$
- Upgrading
pip
andwheel
. Sometimes your system python installs outdated versions of package management tools. Run the following command to update these tools(project-name) [user@local ~]$ pip install --upgrade pip wheel
- Install packages manually
(project-name) [user@local ~]$ pip install package1 package2
- OR install packages from a
requirements.txt
file(project-name) [user@local project]$ pip install -r requirements.txt
If you want to share your current environment with collaborators, you can create a requirements.txt file
(project-name) [user@local project]$ pip freeze > requirements.txt
Data science
- numpy
- pandas
- sklearn
- scipy
- statsmodels
Visualization
- matplotlib
- seaborn
- Navigate to the directory containing the script you want to run.
- Run a standalone python script
script.py
(project-name) [user@local project]$ python script.py
- If you want to run the script interactively so that you have access to a python shell after the script finishes
(project-name) [user@local project]$ python -i script.py
- Install the
jupyter
package if it is not already installed(project-name) [user@local ~]$ pip install jupyter
- Start the jupyter notebook server
(project-name) [user@local project]$ jupyter notebook [I 23:00:50.504 NotebookApp] Serving notebooks from local directory: /home/user/project [I 23:00:50.504 NotebookApp] The Jupyter Notebook is running at: [I 23:00:50.504 NotebookApp] http://localhost:8888/?token=da365240ef9e8c5b5b72c75097e823dcf256c137fa18f31e [I 23:00:50.504 NotebookApp] or http://127.0.0.1:8888/?token=da365240ef9e8c5b5b72c75097e823dcf256c137fa18f31e [I 23:00:50.504 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 23:00:50.517 NotebookApp] To access the notebook, open this file in a browser: file:///home/user/.local/share/jupyter/runtime/nbserver-30400-open.html Or copy and paste one of these URLs: http://localhost:8888/?token=da365240ef9e8c5b5b72c75097e823dcf256c137fa18f31e or http://127.0.0.1:8888/?token=da365240ef9e8c5b5b72c75097e823dcf256c137fa18f31e
This will automatically open your web browser and navigate to the correct URL
(project-name) [user@local ~]$ pip install jupyter_contrib_nbextensions
This only installs the extensions but does not let you enable them. Copy the extensions to the jupyter server's search directory
(project-name) [user@local ~]$ jupyter contrib nbextension install --user
Start a SSH tunnel as a background process
ssh -NfL localhost:8888:localhost:8888 user@remote
SSH into the remote host and start the jupyter notebook server
[user@local ~] ssh user@remote
[user@remote ~] source ~/venvs/project-name/bin/activate
(project-name) [user@remote ~] jupyter notebook --no-browser
Open the displayed URL in a browser on your local machine