Last modified 4 months ago Last modified on 05/26/22 08:04:24

Installing sunPy in a python virtual environment

A python virtual environment on linux or MacOS is a good way to experiment in python without affecting the system setup of python. Modules can be installed aside from the system version. Afterwards, the virtual environment can be deleted. Here, we install sunPy and use it in a simple example.

First, make sure you have a fairly recent version of python. At the bare minimum, make sure it's release 3 or more :

$ python --version
Python 3.9.6

Note that in order to get python version three, on some systems you may have to use "python3" rather than "python" for all commands.

First, build a virtual environment in an appropriately name directory and enter into it by sourcing the activation file :

$ python -m venv /home/oien/johnBsunPy
$ source /home/oien/johnBsunPy/bin/activate

At this point the system prompt will change to reflect the fact that you're in the virtual environment. To be sure you're in the virtual environment, check that the virtual environment versions of python and pip are being used :

(johnBsunPy) $ which python
(johnBsunPy) $ which pip

That looks good. Now upgrade pip to the latest version, and install sunPy :

(johnBsunPy) $ pip install --upgrade pip
(johnBsunPy) $ pip install "sunpy[all]"

You can now use sunPy in the virtual environment :

(johnBsunPy) $ python
Python 3.9.6 (default, Jun 30 2021, 10:22:16) 
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from import Fido, attrs as a
>>> result ='2021/02/02 16:00:00', '2021/02/02 16:30:00'), a.Instrument('big bear'), a.Physobs('intensity'))
>>> print("Provider","Source","fileurl"))
Results from 1 Provider:

14 Results from the VSOClient:
Provider Source                               fileurl                              
-------- ------ -------------------------------------------------------------------
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG
     NSO   GONG

>>> exit()

You can leave the virtual environment with the "deactivate" command at the linux command line prompt, at which point your prompt will change back :

(johnBsunPy) $ deactivate

Another example search, this one using units and sampling :

from import Fido, attrs as a
import astropy.units as u

result =, a.Wavelength(30.4 * u.nm), a.Time('2022/02/23 19:00:00', '2022/02/23 21:00:00'), a.Sample(0.5*u.hour))