Building PyCrypto with fastmath (gmp or mpir) via pip on Windows

2024/9/20 6:13:49

I installed PyCrypto on Windows via pip but i was not able to build Crypto.PublicKey._fastmath because GMP was not found.

I know there is a binary version on voidspace but i would like to build the latest version of PyCrypto

Answer

The following one is a way to achieve your goal. There are other, probably better ways (e.g. based on Visual Studio), but this one has worked for me. Additionally, it does not use pip.

All operations are carried out on a command prompt.

  1. Install Mingw, including MSYS and the Development Toolkit. This will give you a fairly complete Unix-like development environment.
  2. Ensure that Mingw binaries are in PATH environment variable. You need MinGW\bin and MingGW\msys\1.0\bin.
  3. Download MPIR sources in a temporary directory. It is important you do not use 2.5.1 because of a bug that will break the build. 2.5.0 is fine.
  4. Build the MPIR library. This is fairly straightforward: execute bash configure followed by make.
  5. HACK #1 Copy libmpir.a from mpir-2.5.0\.libs into C:\Python2.7.1\libs. This is necessary because distutils is broken and I could not find a way to direct it to the correct library location.
  6. HACK #2 Edit C:\Python2.7.1\Lib\distutils\cygwincompiler.py and remove any occurrance of the string -mno-cygwin. The reason is explained here.
  7. Download PyCrypto sources and unpack them in another temporary directory.
  8. Set CPPFLAGS environment variable to the MPIR directory, which contains mpir.h.
  9. HACK 3 Edit setup.py and add the following line in build_extension method:

    self.__add_compiler_option(os.environ['CPPFLAGS'])

  10. Run bash configure. You should see two lines saying:

    checking for __gmpz_init in -lgmp... no
    checking for __gmpz_init in -lmpir... yes

  11. Execute python setup.py build -c mingw32. You should see no errors.
  12. Execute python setup.py test to verify that everything is fine.
  13. Execute python setup.py install to copy the files into your local Python repository.
  14. Alternatively, run python setup.py bdist_wininst to create an installer.

I really hate all the various hacks, and I'd love to hear if they can be avoided.

https://en.xdnf.cn/q/72204.html

Related Q&A

Get name of current test in setup using nose

I am currently writing some functional tests using nose. The library I am testing manipulates a directory structure. To get reproducible results, I store a template of a test directory structure and cr…

python: find html tags and replace their attributes [duplicate]

This question already has answers here:Replace SRC of all IMG elements using Parser(2 answers)Closed 10 years ago.I need to do the following:take html document find every occurrence of img tag take the…

Django/Apache/mod_wsgi not using virtualenvs Python binary

I have a virtualenv at /opt/webapps/ff/ with its own Python installation. I have WSGIPythonHome set to /opt/webapps/ff in my Apache config file (and this is definitely getting used in some capacity, b…

How to open the users preferred mail application on Linux?

I wrote a simple native GUI script with python-gtk. Now I want to give the user a button to send an email with an attachment.The script runs on Linux desktops. Is there a way to open the users preferr…

finding a set of ranges that a number fall in

I have a 200k lines list of number ranges like start_position,stop position. The list includes all kinds of overlaps in addition to nonoverlapping ones.the list looks like this[3,5] [10,30] [15,25] [5…

Python Tornado Websocket Connections still open after being closed

I have a Tornado Websocket Server and I want to time out after 30 minutes of inactivity. I use self.close() to close the connection after 30 minutes of inactivity. But it seems that some connections st…

Vertical Print String - Python3.2

Im writing a script that will take as user inputed string, and print it vertically, like so:input = "John walked to the store"output = J w t t so a o h th l e on k re edIve written …

How to remove small particle background noise from an image?

Im trying to remove gradient background noise from the images I have. Ive tried many ways with cv2 without success.Converting the image to grayscale at first to make it lose some gradients that may hel…

Running commands from within python that need root access

I have been playing around with subprocess lately. As I do more and more; I find myself needing root access. I was wondering if there is an easy way to enter the root password for a command that needs …

How not to plot missing periods

Im trying to plot a time series data, where for certain periods there is no data. Data is loaded into dataframe and Im plotting it using df.plot(). The problem is that the missing periods get connected…