Skip to content

Commit 32edad0

Browse files
seismanweiji14
andauthored
doc: Convert the installation guides source code from ReST to Markdown (#2992)
Co-authored-by: Wei Ji <[email protected]>
1 parent 621d6da commit 32edad0

File tree

4 files changed

+295
-313
lines changed

4 files changed

+295
-313
lines changed

doc/conf.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
# ruff: isort: on
1414

15+
requires_python = metadata("pygmt")["Requires-Python"]
16+
with pygmt.clib.Session() as lib:
17+
requires_gmt = f">={lib.required_version}"
18+
1519
extensions = [
1620
"myst_parser",
1721
"sphinx.ext.autodoc",
@@ -34,10 +38,18 @@
3438

3539
# Auto-generate header anchors with MyST parser
3640
myst_heading_anchors = 4
41+
# reference: https://myst-parser.readthedocs.io/en/latest/syntax/optional.html
3742
myst_enable_extensions = [
3843
"attrs_inline", # Allow inline attributes after images
3944
"colon_fence", # Allow code fences using colons
45+
"substitution", # Allow substituitions
4046
]
47+
# These enable substitutions using {{ key }} in the Markdown files
48+
myst_substitutions = {
49+
"requires_python": requires_python,
50+
"requires_gmt": requires_gmt,
51+
}
52+
4153

4254
# Make the list of returns arguments and attributes render the same as the
4355
# parameters list
@@ -151,17 +163,6 @@
151163
html_baseurl = "https://pygmt.org/latest/"
152164
release = __version__
153165

154-
requires_python = metadata("pygmt")["Requires-Python"]
155-
with pygmt.clib.Session() as lib:
156-
requires_gmt = ">=" + lib.required_version
157-
158-
# These enable substitutions using |variable| in the rst files
159-
rst_epilog = f"""
160-
.. |year| replace:: {year}
161-
.. |requires_python| replace:: {requires_python}
162-
.. |requires_gmt| replace:: {requires_gmt}
163-
"""
164-
165166
html_last_updated_fmt = "%b %d, %Y"
166167
html_title = "PyGMT"
167168
html_short_title = "PyGMT"

doc/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
:caption: Getting Started
2020

2121
overview.md
22-
install.rst
22+
install.md
2323
get_started/index.rst
2424

2525
.. toctree::

doc/install.md

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
# Installing
2+
3+
## Quickstart
4+
5+
The fastest way to install PyGMT is with the [mamba](https://mamba.readthedocs.io/en/latest/)
6+
or [conda](https://docs.conda.io/projects/conda/en/latest/user-guide/index.html)
7+
package manager which takes care of setting up a virtual environment, as well as the
8+
installation of GMT and all the dependencies PyGMT depends on:
9+
10+
:::: {tab-set}
11+
::: {tab-item} mamba
12+
:sync: mamba
13+
```
14+
mamba create --name pygmt --channel conda-forge pygmt
15+
```
16+
:::
17+
18+
::: {tab-item} conda
19+
:sync: conda
20+
```
21+
conda create --name pygmt --channel conda-forge pygmt
22+
```
23+
:::
24+
::::
25+
26+
To activate the virtual environment, you can do:
27+
28+
:::: {tab-set}
29+
::: {tab-item} mamba
30+
:sync: mamba
31+
```
32+
mamba activate pygmt
33+
```
34+
:::
35+
36+
::: {tab-item} conda
37+
:sync: conda
38+
```
39+
conda activate pygmt
40+
```
41+
:::
42+
::::
43+
44+
After this, check that everything works by running the following in a Python interpreter
45+
(e.g., in a Jupyter notebook):
46+
```python
47+
import pygmt
48+
pygmt.show_versions()
49+
```
50+
51+
You are now ready to make you first figure! Start by looking at the tutorials on our
52+
sidebar, good luck!
53+
54+
:::{note}
55+
The sections below provide more detailed, step by step instructions to install and test
56+
PyGMT for those who may have a slightly different setup or want to install the latest
57+
development version.
58+
:::
59+
60+
## Which Python?
61+
62+
PyGMT is tested to run on Python {{ requires_python }}.
63+
64+
We recommend using the [Miniforge](https://github.com/conda-forge/miniforge#miniforge3)
65+
Python distribution to ensure you have all dependencies installed and
66+
the [mamba](https://mamba.readthedocs.io/en/stable/user_guide/mamba.html) package manager
67+
in the base environment. Installing Miniforge does not require administrative rights to
68+
your computer and doesn't interfere with any other Python installations on your system.
69+
70+
## Which GMT?
71+
72+
PyGMT requires Generic Mapping Tools (GMT) {{ requires_gmt }} since there are many
73+
changes being made to GMT itself in response to the development of PyGMT.
74+
75+
Compiled conda packages of GMT for Linux, macOS and Windows are provided through
76+
[conda-forge](https://anaconda.org/conda-forge/gmt). Advanced users can also
77+
[build GMT from source](https://github.com/GenericMappingTools/gmt/blob/master/BUILDING.md)
78+
instead.
79+
80+
We recommend following the instructions further on to install GMT 6.
81+
82+
## Dependencies
83+
84+
PyGMT requires the following libraries to be installed:
85+
86+
- [numpy](https://numpy.org) (>= 1.23)
87+
- [pandas](https://pandas.pydata.org)
88+
- [xarray](https://xarray.dev/)
89+
- [netCDF4](https://unidata.github.io/netcdf4-python)
90+
- [packaging](https://packaging.pypa.io)
91+
92+
The following are optional dependencies:
93+
94+
- [IPython](https://ipython.org): For embedding the figures in Jupyter notebooks (recommended).
95+
- [Contextily](https://contextily.readthedocs.io): For retrieving tile maps from the internet.
96+
- [GeoPandas](https://geopandas.org): For using and plotting GeoDataFrame objects.
97+
- [RioXarray](https://corteva.github.io/rioxarray): For saving multi-band rasters to GeoTIFFs.
98+
99+
:::{note}
100+
If you have [PyArrow](https://arrow.apache.org/docs/python/index.html) installed, PyGMT
101+
does have some initial support for `pandas.Series` and `pandas.DataFrame` objects with
102+
Apache Arrow-backed arrays. Specifically, only uint/int/float and date32/date64 dtypes
103+
are supported for now. Support for string Arrow dtypes is still a work in progress.
104+
For more details, see [issue #2800](https://github.com/GenericMappingTools/pygmt/issues/2800).
105+
:::
106+
107+
## Installing GMT and other dependencies
108+
109+
Before installing PyGMT, we must install GMT itself along with the other dependencies.
110+
The easiest way to do this is via the `mamba` or `conda` package manager. We recommend
111+
working in an isolated
112+
[virtual environment](https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)
113+
to avoid issues with conflicting versions of dependencies.
114+
115+
First, we must configure conda to get packages from the [conda-forge channel](https://conda-forge.org/):
116+
```
117+
conda config --prepend channels conda-forge
118+
```
119+
120+
Now we can create a new virtual environment with Python and all our dependencies
121+
installed (we'll call it `pygmt` but feel free to change it to whatever you want):
122+
123+
:::: {tab-set}
124+
::: {tab-item} mamba
125+
:sync: mamba
126+
```
127+
mamba create --name pygmt python=3.12 numpy pandas xarray netcdf4 packaging gmt
128+
```
129+
:::
130+
131+
::: {tab-item} conda
132+
:sync: conda
133+
```
134+
conda create --name pygmt python=3.12 numpy pandas xarray netcdf4 packaging gmt
135+
```
136+
:::
137+
::::
138+
139+
Activate the environment by running the following (**do not forget this step!**):
140+
141+
:::: {tab-set}
142+
::: {tab-item} mamba
143+
:sync: mamba
144+
```
145+
mamba activate pygmt
146+
```
147+
:::
148+
149+
::: {tab-item} conda
150+
:sync: conda
151+
```
152+
conda activate pygmt
153+
```
154+
:::
155+
::::
156+
157+
From now on, all commands will take place inside the virtual environment called `pygmt`
158+
and won't affect your default `base` installation.
159+
160+
## Installing PyGMT
161+
162+
Now that you have GMT installed and your virtual environment activated, you can install
163+
PyGMT using any of the following methods.
164+
165+
### Using mamba/conda (recommended)
166+
167+
This installs the latest stable release of PyGMT from [conda-forge](https://anaconda.org/conda-forge/pygmt):
168+
169+
:::: {tab-set}
170+
::: {tab-item} mamba
171+
:sync: mamba
172+
```
173+
mamba install pygmt
174+
```
175+
:::
176+
177+
::: {tab-item} conda
178+
:sync: conda
179+
```
180+
conda install pygmt
181+
```
182+
:::
183+
::::
184+
185+
This upgrades the installed PyGMT version to be the latest stable release:
186+
187+
:::: {tab-set}
188+
::: {tab-item} mamba
189+
:sync: mamba
190+
```
191+
mamba update pygmt
192+
```
193+
:::
194+
195+
::: {tab-item} conda
196+
:sync: conda
197+
```
198+
conda update pygmt
199+
```
200+
:::
201+
::::
202+
203+
### Using pip
204+
205+
This installs the latest stable release from [PyPI](https://pypi.org/project/pygmt):
206+
```
207+
python -m pip install pygmt
208+
```
209+
210+
::: {tip}
211+
You can also run `python -m pip install pygmt[all]` to install pygmt with all of its
212+
optional dependencies.
213+
:::
214+
215+
Alternatively, you can install the latest development version from
216+
[TestPyPI](https://test.pypi.org/project/pygmt):
217+
```
218+
python -m pip install --pre --extra-index-url https://test.pypi.org/simple/ pygmt
219+
```
220+
221+
To upgrade the installed stable release or development version to be the latest one,
222+
just add `--upgrade` to the corresponding command above.
223+
224+
Any of the above methods (mamba/conda/pip) should allow you to use the PyGMT package
225+
from Python.
226+
227+
## Testing your install
228+
229+
To ensure that PyGMT and its dependencies are installed correctly, run the following
230+
in your Python interpreter:
231+
232+
```python
233+
import pygmt
234+
pygmt.show_versions()
235+
236+
fig = pygmt.Figure()
237+
fig.coast(region="g", frame=True, shorelines=1)
238+
fig.show()
239+
```
240+
241+
If you see a global map with shorelines, then you're all set.
242+
243+
## Common installation issues
244+
245+
If you have any issues with the installation, please check out the following common
246+
problems and solutions.
247+
248+
### "Error loading GMT shared library at ..."
249+
250+
Sometimes, PyGMT will be unable to find the correct version of the GMT shared library
251+
(`libgmt`). This can happen if you have multiple versions of GMT installed.
252+
253+
You can tell PyGMT exactly where to look for `libgmt` by setting the `GMT_LIBRARY_PATH`
254+
environment variable to the directory where `libgmt.so`, `libgmt.dylib` or `gmt.dll` can
255+
be found on Linux, macOS or Windows, respectively.
256+
257+
For Linux/macOS, add the following line to your shell configuration file (usually
258+
`~/.bashrc` for Bash on Linux and `~/.zshrc` for Zsh on macOS):
259+
```
260+
export GMT_LIBRARY_PATH=$HOME/miniforge3/envs/pygmt/lib
261+
```
262+
263+
For Windows, add the `GMT_LIBRARY_PATH` environment variable following these
264+
[instructions](https://www.wikihow.com/Create-an-Environment-Variable-in-Windows-10)
265+
and set its value to a path like:
266+
```
267+
C:\Users\USERNAME\Miniforge3\envs\pygmt\Library\bin\
268+
```
269+
270+
### `ModuleNotFoundError` in Jupyter notebook environment
271+
272+
If you can successfully import pygmt in a Python interpreter or IPython, but get a
273+
`ModuleNotFoundError` when importing pygmt in Jupyter, you may need to activate your
274+
`pygmt` virtual environment (using `mamba activate pygmt` or `conda activate pygmt`)
275+
and install a `pygmt` kernel following the commands below:
276+
```
277+
python -m ipykernel install --user --name pygmt # install virtual environment properly
278+
jupyter kernelspec list --json
279+
```
280+
281+
After that, you need to restart Jupyter, open your notebook, select the `pygmt` kernel
282+
and then import pygmt.

0 commit comments

Comments
 (0)