Install Python Packages¶
This page describes how to install a Python package in a CoCalc project.
CoCalc already includes hundreds of packages for several Python environments. Check first if the lib you’re looking for is already installed!
If a package may have general use but is not already installed in CoCalc, please open a support request to tell us to install it globally for everyone. Uncomplicated install requests are typically handled within 1 business day for paying customers. Install will happen faster if you include as much as possible of the following information:
- Which Python environment?
- Which language version: 2 or 3?
- A link to the package website
- Special requirements and dependencies to build & install
- A short but complete example, such that we can verify that we properly installed the software. This example might be included in internal tests, to make sure future updates do not break that library.
Your project must have the “Internet access” upgrade in order to download software from a remote repository (e.g. PyPI or Anaconda) to your project. The install command will not work unless you upgrade your project to have internet access.
A way to work around such a blocked internet access is to upload the package files into your project.
You can install additional packages yourself, but only at user-permission level.
CoCalc accounts do not have superuser (root) privileges.
Software must be installed into user-writeable parts of the filesystem, which are in
/home/user (check the value of
In a nutshell: a CoCalc project is a Linux user account under the username
Therefore, installing software and libraries should usually be done in
which is the canonical location for user installs.
Furthermore, in case the documentation mentions to specify a custom “prefix” path,
set this to
Executables will install into
~/.local/bin and will work right away,
because projects already include that path in their
In the case of Python 2,
$HOME/.local/lib/python2.7/site-packages/ will contain the package you’ve installed.
Similarly, this path will contain
python3.5 for a Python 3.5 executable.
In case your Python environment can’t find the package,
you might have to add your
~/.local/... directory dynamically during runtime like that:
import sys, os sys.path.insert(0, os.path.expanduser('~/.local/lib/python2.7/site-packages'))
Make sure, the path is correct.
I.e. for Python 3 this could be one of
Pip is the “Python package manager”. It installs packages hosted at PyPI.org.
If your package can be installed via
then run in a CoCalc Terminal file:
pip2 install --user [package-name]
pip3 install --user [package-name]
Regarding Python 2 vs. Python 3:
- Python 2: use
pythonshould default to these variants.
- Python 3: use
If you’ve uploaded a zip/wheel file,
[package-name] to the actual filename.
If your package is in a folder inside your project
(e.g., you uploaded it) which includes a
you can do either
python setup.py install --user or
pip install --user --upgrade ./
(Some setup instructions alternatively mention
python setup.py install --home)
If pip requires that any external dependencies be downloaded, then your project must have internet access.
You can avoid the need for
--user flags if you work inside a Python virtual environment.
See Virtualenv for more information.
A special case is [SageMath], which is a fully integrated environment built on top of Python.
To install a Python package in Sage, it needs to also install into your local home directory.
To accomplish that, first start the Sage-environment in a Terminal, and then issue the pip-install command with
--user. For example:
sage -shfor the sage environemnt
pip install --user git+https://github.com/videlec/sage-flatsurf
If it happens that Sage doesn’t recognize packages in your local path, prepend them to your path via running
import site, sys sys.path.insert(0, site.USER_SITE)
sage -sh environment, you can also run
R to install additional R packages in Sage. This also works for other programming libraries.
You can also combine step (1) and (2) via
sage --pip install --user ...
In case you run a Sage Worksheet, you need to restart the worksheet server (in the project settings) and then the worksheet itself via the Restart button.
CoCalc provides a global environmet, which you can start by running
anaconda2019 in a Linux Terminal or a related kernel in a Jupyter Notebooks.
To get going with your own setup for your own CoCalc project,
you have to create your own environment
and your own kernel.
The task below is to create a custom Anaconda overlay environment called
myconda and, just for the sake of explanation,
- install “Microsoft’s Open R” (which is an enhanced version of R by Microsoft).
- Install the plotly library from PyPI
To get it installed in Anaconda as a user, do this:
Open a terminal.
conda create -n myconda -c mro rThis creates a new local environment called “myconda” (name it as you wish) with the package “r” as its source coming from the channel “mro” (Microsoft’s Open R). Instead of that, you can add any other anaconda package in that spot. The example from the documentation is biopython, see http://conda.pydata.org/docs/using/envs.html#create-an-environment.
When installing, it briefly shows you that it ends up in
~/.conda/envs/myconda/....in your local files. Now that we have it installed, we can get out of this “root” environment via source deactivate or restart the session. In any case, you are back in the the normal Linux terminal environment.
Now run this:
source ~/.conda/envs/myconda/bin/activate mycondaNote that myconda is the name specified above, and the prompt switches to
(myconda) $. Typing
/projects/xxx-xxx-xxx/.conda/envs/myconda/bin/Rand of course, just running
R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree" Copyright (C) 2015 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) [...] Microsoft R Open 3.2.3 Default CRAN mirror snapshot taken on 2016-01-01 The enhanced R distribution from Microsoft
In the very same spirit, you can also run pip installations:
(myconda)~$ pip install plotly Downloading/unpacking plotly [...] Successfully installed plotly requests six pytz (myconda)~$ python -c 'import plotly; print(plotly)' <module 'plotly' from '/projects/20e4a191-73ea-4921-80e9-0a5d792fc511/.local/lib/python2.7/site-packages/plotly/__init__.pyc'>
Note that since I’m still in my own “myconda” overlay environment, the
--user switch in
pip install wasn’t necessary. (Otherwise, it would be necessary.)
conda environment and software manager, you can create custom environments with specific versions of Python, R, and their packages. This is similar to capabilities provided by Python’s environment manager, Virtualenv.
Suppose you want to create a custom Anaconda environment with the
mdtraj package and be able to use this environment in a Jupyter notebook. Here’s how:
Follow these steps in a .term file in CoCalc. In the last step, the display name of the new kernel is changed so that it does not duplicate the name of kernel installed by CoCalc:
~$ mkdir -p ~/.local/share/jupyter/kernels ~$ anaconda2019 (root) ~$ conda create --name mymdtraj mdtraj (root) ~$ source activate mymdtraj (mymdtraj) ~$ conda install ipykernel (mymdtraj) ~$ source deactivate ~$ mv ~/.conda/envs/mymdtraj/share/jupyter/kernels/python3 ~/.local/share/jupyter/kernels/mymdtraj ~$ open ~/.local/share/jupyter/kernels/mymdtraj/kernel.json ## change display_name from "Python 3" to "My mdtraj" and save the file
Open a new Jupyter notebook in CoCalc.
Click on the Kernel button and look for your new kernel, “My mdtraj”, or whatever you used for
kernel.json. If you don’t see your new kernel, scroll to the bottom of the Kernel menu and click Refresh Kernel List, and your new kernel should appear.
Select the new kernel. You will now be running the environment you created with the