1

I am following this answer from a post about compiling Cython to exe.

Here is the what python prints when I start it (for version):

Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)] on win32

and for gcc --version:

gcc (rubenvb-4.7.2-release) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.

I have two files for compilation: setup.py and hello.pyx.

hello.pyx:

cdef char* say_hello_to(char* name):
    return "Hello %s!" % name
# I know this may not work but removing it does
# not make a difference in the error message
print say_hello_to("bob")

setup.py:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    name = 'Hello world app',
    ext_modules = cythonize("hello.pyx"),
)

I ran cython hello.pyx --embed without errors and produced a hello.c file. Then, as I am running on Windows 10 64-bit AMD, I compiled with this (no errors raised):

gcc hello.c -I \Python27\include\ -L \Python27\libs\ -l python27 -o test.exe -D MS_WIN64

However, when I run test.exe in my command prompt, it will show an error:

ImportError: No module named site

I googled it, and the closest question I could find was Python (Windows) - ImportError: No module named site. The answers suggested that I set PYTHONPATH, PYTHONHOME and PATH. Which I did accordingly with the following few commands:

SET PYTHONHOME=C:\Python27
SET PYTHONPATH=C:\Python27\Lib
SET PATH=%PYTHONHOME%;%PYTHONPATH%;C:\MinGW\bin\;C:\MinGW;C:\Python27\Scripts;

Unfortunately, after doing this, the same error still occurred.

How do I resolve this ImportError?

Community
  • 1
  • 1
Moon Cheesez
  • 2,489
  • 3
  • 24
  • 38
  • Are you using buggy 2.7.11? If so `set "PYTHONHOME=C:\Python27" & test.exe` should have solved it, assuming that's where Python is installed. This shouldn't be needed in 2.7.10. See issues [26108](http://bugs.python.org/issue26108) and [25824](http://bugs.python.org/issue25824). There's no need to modify `PATH`. Also, with the home directory set, Python builds its default `sys.path` without needing to set `PYTHONPATH` either. – Eryk Sun Jun 01 '16 at 17:43
  • Your example works for me when I build it using [Visual C++ Compiler for Python 2.7](https://www.microsoft.com/en-us/download/details.aspx?id=44266) and link with 2.7.10. – Eryk Sun Jun 01 '16 at 17:46
  • @eryksun I removed `SET PYTHONPATH` and `SET PATH` but there error was still raised. I looked at the issues you linked but didn't understand how to find the DLL version string. However, I did have a 32-bit and 64-bit problem previously and installed 64-bit over my 32-bit python. For your second comment, I am not sure if I am using Visual C++ compiler to compile but I had installed it. The problem may lie in my gcc as it is ruby's. I'm not sure how to compile with the Visual C++ compiler though. – Moon Cheesez Jun 02 '16 at 01:56
  • If it's installed, then your start menu should have a folder named "Microsoft Visual C++ Compiler Package for Python 2.7". Run the 64-bit command prompt. Then compile using `cl /c /I "C:\Python27\Include" hello.c` and link using `link /LIBPATH:"C:\Python27\libs" /OUT:test.exe hello.obj python27.lib`. – Eryk Sun Jun 02 '16 at 07:28
  • @eryksun I found the folder named "Microsoft Visual C++ Compiler Package for Python 2.7" with two 64-bit command prompts. I ran the commands you gave with Visual C++ 2008 64-bit Command Prompt and Visual C++ 2008 64-bit Cross Tools Command Prompt, but the same error occurred. – Moon Cheesez Jun 02 '16 at 08:38
  • You can try removing your current version of 2.7 to install [64-bit 2.7.10](https://www.python.org/ftp/python/2.7.10/python-2.7.10.amd64.msi). – Eryk Sun Jun 02 '16 at 09:06

0 Answers0