0

I have two simple files: main.py and config_cowsay.py

main.py

import cowsay
import config_cowsay

char = config_cowsay.choose_character()
print(char)
print(cowsay.get_output_string(char, "Raptors are evil"))

config_cowsay.py

from random import *

def choose_character():
    n = randint(1, 3)
    switch = {
        1: "trex",
        2: "cow",
        3: "stegosaurus",
    }
    return switch.get(n, "daemon")

If I run main.py, the program executes correctly. Now I need to transform config_cowsay into a pyd file and still be able to call it from main.py. I used MSYS MinGW 32-bit with a makefile to do it (linking the makefile doesn't add anything to the problem resolution I think but I can do it if required). Executing my Makefile from MSYS MinGW 32-bit terminal generates me a folder with the previous main.py and a config_cowsay.pyd next to it. It also generates a main.exe as a one file executable in another folder.

 root  
   |-src
      |-main.py
      |-config_cowsay.py
   |-test_cowsay
      |-main.py
      |-config_cowsay.pyd 
   |-dist
      |-main.exe

So now, if I run the exe from a powershell terminal, the program runs perfectly. If I run the main.py file next to the config_cowsay.pyd using the MSYS MinGW 32-bit terminal, the main.py script executes perfectly using the pyd module and without having to change anything in the main.py code. But if I try to run main.py with my powershell terminal using python main.py, I get an error telling me that the module config_cowsay doesn't exist.

module not found

I already tried everything I could find on this subject. Adding the pyd file to the sys.path variable, editing PYTHONPATH environment variable, moving the pyd dll to site-package or DLLs python folder, renaming config_cowsay.pyd to config_cowsay_p.pyd etc...

I made sure that the environment of MSYS is the same as my computer, the same python version 3.8.9 32 bit with the same modules installed etc... So it shouldn't be a python version issue. On top of that, running the output main.exe from my powershell terminal works, meaning my computer environment should be compatible with the MSYS one I used to generate the pyd dll.

Let me know if you need more details. Thanks for your help!

Jyothi Kiranmayi
  • 2,090
  • 5
  • 14
Erhode
  • 152
  • 8
  • python will find a module if is in the current directory or in sys.path. To add a directory do `sys.path.append('mydir')` before trying to import it. – stark Jun 24 '21 at 15:04
  • Already tried this without success :/ I think I tried everything possible with path manipulation I could think of. Not to mention the pyd file is next to the py file in my example – Erhode Jun 24 '21 at 18:25

1 Answers1

1

I got it to work by getting read of MSYS environment and using directly my computer python environment. I installed python globally on the computer and used a setup file (https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html) instead of a makefile.

Erhode
  • 152
  • 8