Before compiling in Windows - opengeostat/pygslib GitHub Wiki
Compiling pygslib in windows
Note: The explanation below is obsolete. The easy way to compile in Windows in August 2022 is installing Miniconda and running the following commands
conda create --name pygslib-build python=3.9
conda activate pygslib-build
conda install cython setuptools numpy VC=14 vs2015_runtime libpython pytest m2w64-toolchain numpy pandas matplotlib scipy vtk colour bokeh ezdxf jupyter jupyterlab ipython
Note that m2w64-toolchain
contains gfortran and libgfortran. Other key libraries are VC=14
, vs2015_runtime
, and libpython.
This page explains how to prepare prerequisites to compile pygslib
for
Python3.8 in Windows. The support for older Python versions is now dropped. This is valid only for Python distribution
compiled with MSC 64 bits, which are the one distributed in the
official python website and other popular
distributions such as Anaconda.
Compiling pygslib in Linux is an easy task. All GNU compilers are installed by default and are fully compatible with most python distributions.
Compiling pygslib in Mac is not that easy anymore. You will require gfortran_osx-64, available as a conda package, and mac XCODE.
Update setuptools
You may update setuptools before traying to compile pygslib. Try this
conda update setuptools
if you are a conda
user.
Installing MSVC compiler
Any Python package with compiled code may match the Python interpreter used. Most python interpreters for windows are compiled with Microsoft visual studio C compiler. You will need the right Microsoft VC runtime libraries installed. However, the compilation of your modules can be with alternative compilers such as GNU GCC and gfortran.
Check requirements in the official python wiki. You may want to install the compact version Visual C++ 14.0 to compile pygslib for Python 3.8.
Installing MSVC compiler for Python 3.8
Install the community version of Microsoft Build Tools for Visual Studio 2017.
Install the Mingw-w64 compiler
You will require gfortran
to compile the code in Fortran and optionally gcc
to
compile C code generated by Cython and f2py. This is the tricky part. You have to make
sure your compiler uses the right libgfortran version if you are planning to distribute
the code. For example, pygslib v 0.0.0.6.3 uses the conda package m2w64-gcc-libgfortran
as a dependency; this contains libgfortran-3.dll. I used Mingw-64 compiler x86_64-6.4.0-posix-seh.
Mingw-64 version 7 and above may contain libgfortran-4.dll, so you have to make sure you use the
right version of Mingw-64 to compile pygslib. Mingw-64 is available at Mingw-w64 oficial page.
Broken dependencies of the Fortran compiled code produce a
DLL load failed
error. You can use Dependencies_x64 (for Windows) program to find the missing DLL.
Make sure the path to mingw64\bin\
is in the windows system environment variable
PATH
. There are two ways of testing this:
- going to the path and making sure the line
C:\Program Files\mingw-w64\x86_64-6.4.0-posix-seh-rt_v5-rev0\mingw64\bin\
is there, and - calling
gfortran
andgcc
and making sure they are recognized by the system. For example, the block of code below shows thatgfortran
was recognized by the system and executed.
c:>gfortran
gfortran: fatal error: no input files
compilation terminated.
Other general prerequisites
For Python 3.8 you may need only vc=14
and vs2015_runtime
. Note that
many popular packages, such as pandas and numpy, depend on these two packages. Conda distributes
vs2015_runtime, and vc=14.1, but you may have it already installed as part of your python distribution.
Pygslib dependencies
To build and run pygslib
make sure you have all the dependencies required, for example:
- numpy
- pandas
- matplotlib
- scipy
- vc =14.1
- vs2015_runtime
- colour
- pyevtk
- ipython
- vtk
- ipython
- bokeh
- jupyter
- ezdxf
see https://github.com/opengeostat/pygslib/blob/master/pygslib_conda/pygslib_Win64_py3.8/meta.yaml to obtain a complete list of dependencies.
Useful notes
The compiler MinGW-w64, for some obscure reason, copy some dependency dll in
the folder .libs
. These files have to be in the gslib folder, along the
compiled extension modules with code in Fortran (with extension *.pyd).
To fix this, we added a script at the end of setup.py
with the code:
# copy dll if so is windows
if os.name=='nt':
mayor = sys.version_info[0]
minor = sys.version_info[1]
os.system('copy build\\lib.win-amd64-{}.{}\\pygslib\\.libs\\*.dll build\\lib.win-amd64-{}.{}\\pygslib\\gslib'.format(mayor,minor,mayor,minor))
os.system('del /s /q build\\lib.win-amd64-{}.{}\\pygslib\\.libs'.format(mayor,minor))
This may duplicate the dll
files in the destination
site-packages\pygslib-x.x.x.x.x
folder after running
python setup.py build
and python setup.py install
.
The solution is not very elegant, but it works!
Important! in Windows you need to build the package before creating a distribution, otherwise the command to copy dependence *.dll will not include theses files in the distribution. For example, to create the wheels (required for) conda packages) you may need to run these two commands:
python setup.py build
python setup.py sdist bdist_wheel