13 กันยายน 2558

Published กันยายน 13, 2558 by with 0 comment

ตั้งค่า C compiler เพื่อคอมไพล์โมดูล Python 3 ใน Windows 64 bit

สวัสดีผู้อ่านทุกท่าน หลาย ๆ ท่านได้ใช้ Python 3 เวชั่น 64 บิตบนเครื่อง Windows แต่เวลาต้องการคอมไพล์โมดูลที่ต้องใช้ภาษาอื่น เช่น โมดูล cython และอื่น ๆ ต้องลำบาณเพราะการตั้งตั้งค่า C compiler อย่าง Microsoft Visual C++ กลับยุ่งยากและซับซ้อน นอกจากนั้น Microsoft Visual C++ ไม่รองรับ 64 บิตอีก ถ้าใช้ Microsoft Visual C++ 10.0  ต้องไปโหลด Windows SDK 7.1 (สำหรับ Windows 7) เพื่อทำให้ Microsoft Visual C++ รองรับการคอมไพล์บน 64 บิต

แต่นั้นทำให้มีปัญหากับระบบปฎิบัติการ Windows รุ่นใหม่ อย่าง Windows 8.1 และ Windows 10 เวชั่น 64 บิต ไม่สามารถทำการติดตั้ง Microsoft Visual C++ 10.0 ได้เลยเพราะไม่สามารถลง Windows SDK 7.1 ได้ ปัญหาการตั้งค่า C compiler กับ Python 3 เวชั่น 64 บิตบน Windows ยุ่งยากเกินไป ทำให้หลาย ๆ คนหันกลับไปใช้ Python 2.7 แทน หรือเปลี่ยนไปรันบนลินุกซ์ ซึ่งสามารถตั้งค่า C compiler ได้อย่างไม่ซับซ้อนมากนัก

บทความนี้ผมได้พบทางออกที่ง่ายกว่าโดยผมไม่ใช้ Microsoft Visual C++ ผมไปใช้ Mingw64 แทน โดยมีรายละเอียดการตั้งค่าต่อไปนี้ครับ
คำเตือน !!!
อย่าลัดหรือข้ามขั้นตอนใด ขั้นตอนหนึ่งไปเด็ดขาด และทางผมไม่ขอรับผิดชอบความเสียหายใด ๆ ที่อาจจะเกิดขึ้นจากการตั้งค่าคอมไพเลอร์ที่ผิดพลาด

Python ถูกคอมไพล์บน Microsoft Visual C++ เวชั่นอะไร

  • Python 3.5 ถูกคอมไพล์บน Visual Studio 2015

  • Python 3.4 ถูกคอมไพล์บน Microsoft Visual C++ เวชั่น 10.0 (คือ Visual Studio 2010)

  • Python 3.3 ถูกคอมไพล์บน Microsoft Visual C++ เวชั่น 10.0 (คือ Visual Studio 2010)

  • Python 3.2 ถูกคอมไพล์บน Microsoft Visual C++ เวชั่น 9.0 (คือ Visual Studio 2008)

  • Python 2.6 - 2.7 ถูกคอมไพล์บน Microsoft Visual C++ เวชั่น 9.0 (คือ Visual Studio 2008)


เมื่อทราบถึงเวชั่นของ Microsoft Visual C++ ที่ทำการคอมไพล์ Python แล้ว มาเริ่มลงมือติดตั้งและตั้งค่า C compiler ให้กับPython 3 เวชั่น 64 บิตบนเครื่อง Windows กันครับ

1. ให้เข้าไปที่ https://bitbucket.org/carlkl/mingw-w64-for-python/downloads

ทำการโหลดไฟล์ mingwpy_amd64_vc***.7z แล้วทำการแตกไฟล์ออกมา
*** คือ เวชั่นของ Microsoft Visual C++ ตัวอย่างเช่น Microsoft Visual C++ เวชั่น 10.0 คือ 100 ชื่อไฟล์จะเป็น mingwpy_amd64_vc100.7z

เมื่อเข้าไปในโฟลเลอร์ที่แตกออกมาจะพบกับโฟลเลอร์ต่าง ๆ หนึ่งในนั้น คือ โฟลเลอร์ bin ให้ทำการตั้งค่า PATH ให้ชี้ไปยังโฟลเลอร์ bin ที่อยู่ในโฟลเลอร์ที่แตกไฟล์ออกมา

[caption id="attachment_1405" align="alignnone" width="1366"]ตั้งค่า PATH ให้ชี้ไปยังโฟลเลอร์ bin ที่อยู่ในโฟลเลอร์ที่แตกไฟล์ออกมา ตั้งค่า PATH ให้ชี้ไปยังโฟลเลอร์ bin ที่อยู่ในโฟลเลอร์ที่แตกไฟล์ออกมา เหมือนกับตอนที่ตั้งค่า path ให้กับไพทอน ตอนติดตั้งไพทอน[/caption]

2. เข้าไปโฟลเลอร์ที่ติดตั้ง Python แล้วเข้าไปแก้ไขที่ไฟล์ Lib/distutils/distutils.cfg (ถ้าไม่มีให้สร้างไฟล์ distutils.cfg ขึ้นมาครับ)

เพิ่มโค้ดนี้ลง

[python][build]
compiler = mingw64[/python]

บันทึกไฟล์ แล้วทำการลบไฟล์ Lib/distutils/cygwinccompiler_orig.py ออก (ถ้ามี)

3. เข้าไปเพิ่มโค้ดลงไปในไฟล์ cygwinccompiler.py ก่อนบรรทัดโค้ด

[python]class Mingw32CCompiler(CygwinCCompiler):[/python]

โค้ดที่เพิ่มลงไป

[python]
class Mingw64CCompiler(CygwinCCompiler):

compiler_type = 'mingw64'

def __init__ (self,verbose=0,dry_run=0,force=0):
CygwinCCompiler.__init__ (self, verbose, dry_run, force)

# ld_version >= "2.13" support -shared so use it instead of
# -mdll -static
if self.ld_version >= "2.13":
shared_option = "-shared"
else:
shared_option = "-mdll -static"

# A real mingw32 doesn't need to specify a different entry point,
# but cygwin 2.91.57 in no-cygwin-mode needs it.
if self.gcc_version <= "2.91.57":
entry_point = '--entry _DllMain@12'
else:
entry_point = ''
self.linker_dll='x86_64-w64-mingw32-gcc'

self.set_executables(compiler='x86_64-w64-mingw32-gcc -O -Wall',
compiler_so='x86_64-w64-mingw32-gcc -mdll -O -Wall',
compiler_cxx='x86_64-w64-mingw32-g -O -Wall',
linker_exe='x86_64-w64-mingw32-gcc',
linker_so='%s %s %s'
% (self.linker_dll, shared_option,
entry_point))
# Maybe we should also append -mthreads, but then the finished
# dlls need another dll (mingwm10.dll see Mingw32 docs)
# (-mthreads: Support thread-safe exception handling on `Mingw32')

# no additional libraries needed
self.dll_libraries=[]

# Include the appropriate MSVC runtime library if Python was built
# with MSVC 7.0 or later.
self.dll_libraries = get_msvcr()

# __init__ ()

# class Mingw64CCompiler
# the same as cygwin plus some additional parameters
[/python]

เสร็จแล้วบันทึกไฟล์

4. ทำการลบไฟล์ Lib/distutils/ccompiler_orig.py ออกจากเครื่อง (ถ้ามี)
5. เพิ่มโค้ดเข้าไปในไฟล์ Lib/distutils/ccompiler.py
โค้ดที่เพิ่ม

[python]
'mingw64': ('cygwinccompiler', 'Mingw64CCompiler',
"Mingw64 port of GNU C Compiler for Win64"),[/python]

เพิ่มไปในระหว่างโค้ด

[python]
"Cygwin port of GNU C Compiler for Win32"),
'mingw32': ('cygwinccompiler', 'Mingw32CCompiler',
"Mingw32 port of GNU C Compiler for Win32"),
'mingw64': ('cygwinccompiler', 'Mingw64CCompiler',
"Mingw64 port of GNU C Compiler for Win64"),
'bcpp': ('bcppcompiler', 'BCPPCompiler',
"Borland C++ Compiler"),
'emx': ('emxccompiler', 'EMXCCompiler',
[/python]

6. เสร็จแล้วทำการโหลดไฟล์ libpython‑X.X.X‑cpZZ‑none‑win_amd64.whl จาก http://www.lfd.uci.edu/~gohlke/pythonlibs/#libpython เปิดคอมมานด์ไลน์เข้าไปยังโฟลเลอร์ที่เก็บไฟล์นี้ไว้ แล้วใช้คำสั่ง
pip install libpython‑X.X.X‑cpZZ‑none‑win_amd64.whl

โดย X.X.X คือ เวชั่นของไพทอน เช่น 3.4.3
ZZ คือ หมายเลขรุ่นไพทอน
ตัวอย่างเช่น หากเป็น Python 3.4.3 64 bit ไฟล์จะเป็น libpython‑3.4.3‑cp34‑none‑win_amd64.whl

7. ลองใช้คำสั่ง pip ติดตั้งโมดูลที่ต้องคอมไพล์ เช่น โมดูล Cython
pip install cython

เสร็จลองเรียกใช้ Cython ด้วยคำสั่ง
cython

หากเรียกใช้งานได้ถือว่าตั้งค่า C compiler กับ Python 64 บิตบนเครื่อง Windows เป็นอันเรียบร้อยแล้วครับ

ติดตามบทความต่อไปนะครับ
ขอบคุณครับ

0 ความคิดเห็น:

แสดงความคิดเห็น

แสดงความคิดเห็นได้ครับ :)