Pebaz / nimporter

Compile Nim Extensions for Python On Import!
MIT License
821 stars 33 forks source link

nimporter ignores nim.cfg #43

Closed mhechthz closed 3 years ago

mhechthz commented 3 years ago

Hi,

I'm totally new to Nim and nimporter and tried to build a little test application. I changed Nim to use clang, which is compatible with CPython on Windows 10. Nim file:

import nimpy
import math, random
import times

let time = cpuTime()

proc mcpi(nthrows: float): float {.exportpy.} =
  randomize()
  var inside = 0.0
  for i in 1..int64(nthrows):
    if hypot(rand(1.0), rand(1.0)) < 1:
      inside += 1
  result = 4 * inside / nthrows

echo mcpi(250000000)

echo cpuTIme()-time  

Python file:

import nimporter, mcpi
import time

time0 = time.time()
print("pi:",mcpi.mcpi(250000000))
print("tm:",time.time()-time0)

Running the nim-file using Nim works perfectly. Compiling to library (nimpy only) and importing to Python also works perfectly.

Running the Python program "hangs" the execution and while trying to start Visual C++. Both files are in the same directory, i.e. there is no directory structure.

What is wrong here?

Pebaz commented 3 years ago

@mhechthz can you post the nim.cfg?

mhechthz commented 3 years ago

It's the default nim.cfg, with the only change

# cc = gcc
cc = clang

How can I be sure, that this config file from Nim installation directory is really used? I set the PATH to NimInstallDir\bin.

EDIT: It seems that LLVM is using MSVC++ during linking. That's probably the reason why I see this running. Nevertheless, it doesn't work

Pebaz commented 3 years ago

Hello @mhechthz I was able to get both of the code snippets to work on my Windows 10 machine using MSVC.

Nimporter can't change Nim's behavior for supporting nim.cfg files so if the nim.cfg is in the proper directory as specified by the docs it will work with Nimporter.

However, you mentioned that "Visual Studio" (the graphical application) starts up when you run the Python example above?

Pebaz commented 3 years ago

Also, Nimporter will tell Nim to use MSVC only if Python was compiled with MSVC. You can tell if Python is compiled with MSVC by running this in a Python shell:

>>> import sys; sys.version
'3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]'

The "MSC" in the square brackets is for MSVC. If "MSC" is not in sys.version, Nimporter does not suggest a compiler, which means that the cc = clang should be honored by Nim itself (since Nimporter will not have asked Nim to use MSVC manually).

Pebaz commented 3 years ago

If you need to explicitly control 100% of all flags Nimporter passes to Nim, you can use a switches.py script.

Pebaz commented 3 years ago

Closing for now