Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

samlambert
Contributor

Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

Hi guys,

 

I'm running into a few errors trying to install M2Crypto on a windows machine. I've done a fair bit of research and it seems to be a common problem with the library.

 

Wondering if anyone here has experienced similar issues and resolved them? I'm using Win10(64 bit), Python 3.8 through Pycharm. Pip 20.1.1. I've tried to install both M2Crypto and M2CryptoWin64 but I've had no luck.

 

Cheers

1 ACCEPTED SOLUTION

Accepted Solutions
samlambert
Contributor

Re: Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

Some builds for m2crypto for specific versions of Python are available from their CI: https://ci.appveyor.com/project/m2crypto/m2crypto/history. Try selecting a version, selecting a job that matches your Python version, then going to the "Artifacts" tab and downloading an installer. To install a .whl file, see step 11 of my build tutorial below.

I have also attached the m2crypto module that I have built on Windows 10 x64, Python 3.8.1. It should work on any x64-based version of Windows with any version of Python 3.8.X. M2Crypto-0.35.2.win-amd64-py3.8.zip

However, if you are unable to find a build that matches your Python version and system type and architecture, you may need to manually build m2crypto. I adapted the build steps from their CI build scripts: https://gitlab.com/m2crypto/m2crypto/blob/master/appveyor.yml. I built the module by doing the following:

  1. Install the latest Build Tools for Visual Studio 2019. See https://visualstudio.microsoft.com/downloads/ under "All Downloads" -> "Tools for Visual Studio 2019". This direct link was active as of this writing: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=16
  2. In the installer, select "C++ Build Tools", install, and reboot if necessary.
  3. Install the latest full (not Light) OpenSSL for your architecture (Win64/Win32). Current version as of this writing is 1.1.1d. Make note of the directory to which you install OpenSSL. https://slproweb.com/products/Win32OpenSSL.html
  4. In PowerShell, install the Chocolatey package manager. I used this command from their website: Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
  5. Install swig with Chocolatey (in PowerShell). choco install -r -y swig
  6. Install the pywin32 dependency. Run pip install pywin32. If you have problems, try first running pip install wheel. To get pip to target a specific Python installation, try launching it using py -[version] -m pip install [module]. Note: you may need to use an elevated (administrator) PowerShell to install Python modules.
  7. Get the latest m2crypto code. If you have git installed, run git clone https://gitlab.com/m2crypto/m2crypto. Otherwise, download and extract the code from GitLab: https://gitlab.com/m2crypto/m2crypto/-/archive/master/m2crypto-master.zip
  8. Use cd to change into the directory m2crypto was cloned/extracted to.
  9. Assuming python launches your desired Python interpreter version, run python setup.py build --openssl="C:\Program Files\OpenSSL-Win64" --bundledlls, replacing C:\Program Files\OpenSSL-Win64 with the directory to which you installed OpenSSL. (On some systems you can use the py launcher to specify a Python version to use, run py -h for more information.)
  10. Generate the installable files. python.exe setup.py bdist_wheel bdist_wininst bdist_msi.
  11. Install the module. cd into the dist directory and run pip install M2Crypto-0.35.2-cp38-cp38-win_amd64.whl, replacing the filename with the generated .whl file. If you have problems, try first running pip install wheel. To get pip to target a specific Python installation, try launching it using py -[version] -m pip install [module]. Alternatively, you can run the generated .exe or .msi installer. Note: you may need to use an elevated (administrator) PowerShell to install Python modules.

View solution in original post

12 REPLIES 12
anirudh
Community Boss

Re: Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

Hi Sam,

 

Try installing the attached library

 

Regards,

Anirudh

samlambert
Contributor

Re: Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

Thanks @anirudh!

 

Do you know how to change the path location when installing it? I want to put it in a venv.

 

Cheers

samlambert
Contributor

Re: Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

Don't worry, figured it out! Thanks again for your help mate

Cheers
samlambert
Contributor

Re: Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

Hi @anirudh 

I've installed M2Crypto successfully but am now getting an error:

 

File "C:\Users\USERNAME\Desktop\PATH\anaplan_auth.py", line 13, in <module>
from M2Crypto import EVP, RSA
File "C:\Users\USERNAME\Desktop\PATH\venv\lib\site-packages\M2Crypto\__init__.py", line 22, in <module>
import __m2crypto
ModuleNotFoundError: No module named '__m2crypto'

 

Have you ran into this error before? I've seen a few Google results with the same error for different things? Once I have this all setup I think I'll create a wiki article for setting up Certificate Authentication on Windows systems.

anirudh
Community Boss

Re: Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

Hi Sam,

 

I had the same issue with my virtual env

Unfortunately, I had to uninstall the whole python instance, get rid of the old M2Crypto library and then reinstall everything with the attached library.....

 

Regards,

Anirudh

 

samlambert
Contributor

Re: Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

I tried to uninstall and re-install but that hasn't worked. I'm still getting the error:

 

File "C:\Users\PATH\TO\FILES\anaplan_auth.py", line 13, in <module>
from M2Crypto import EVP, RSA
File "C:\Users\PATH\TO\FILES\\venv\lib\site-packages\M2Crypto\__init__.py", line 22, in <module>
import __m2crypto
ModuleNotFoundError: No module named '__m2crypto'

 

But __m2crypto.py is in the same directory

anirudh
Community Boss

Re: Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

Hey Sam,

You have to edit the script to say -
'Import M2Crypto'

not __m2crypto

Regards,
Anirudh
samlambert
Contributor

Re: Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

Strange, now I'm getting:

 

File "C:\Users\Sam\Desktop\Data_Integration_Anaplan\anaplan_auth.py", line 13, in <module>
from M2Crypto import EVP, RSA
File "C:\Users\Sam\Desktop\Data_Integration_Anaplan\venv\lib\site-packages\M2Crypto\__init__.py", line 23, in <module>
import m2
ModuleNotFoundError: No module named 'm2'

 

There is an m2.py file in the directory though.

mendaye
Not applicable

Re: Installing M2Crypto Python Library on Windows Machine for Certificate Authentication

Hello Sam - I am running the same issue. Can you please specify steps help you to resolve this issue?

 

Regards

Suraphel