Windows10下anaconda成功运行Cython

前提:已安装了anaconda,cython已安装:conda install -c anaconda cython,后面无法编译老出各种错误:没有***var.bat文件或者gcc.exe failed 等。。。下面是摸索出的正确道路:


1. 下载mingw-get-setup.exe, http://www.mingw.org/wiki/Getting_Started
2. 安装gcc-mingw-4.3.3-setup.exe, https://github.com/develersrl/gccwinbinaries/releases
【上面两步我都做了,其实也不知道有没有用因为我极度怀疑4步骤才是精华,但是mingw对其他语言lua等也作用很大还是安装了。做了步骤2就貌似不用做1,因为2也会安装1】
并且将C:\MinGW\bin加入了windows系统变量,自创文件"Anaconda文件夹下的Lib\distutils\distutils.cfg",内容如下:
[build]
compiler = mingw32

3. 换conda的channel为清华源,不然后面conda install的速度太慢了:
来源 https://mirror.tuna.tsinghua.edu.cn/help/anaconda/
复制下面文字到windows用户目录下的 .condarc 文件

channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

4. conda install libpython m2w64-toolchain -c msys2
【-c msys2写不写无所谓,因为-c指定channel,我们上面设置了default channnel conda会自己找的】

5. 准备setup.py文件:
from distutils.core import setup
from Cython.Build import cythonize
setup(name='Hello world app',
      ext_modules=cythonize("hello.pyx"))
【格式一定要一样,不然可能出现错误】

和hello.pyx:
def say_hello_to(name):
    print("Hello %s!" % name)

6. dos窗口运行:python setup.py build_ext --inplace
无报错

7. 测试:文件hello_yyy.py:
from hello import say_hello_to
say_hello_to("Yang, Yiyan")

dos输出:(base) Your_path\hello>python hello_yyy.py
Hello Yang, Yiyan!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章