[PYTHON] If you have trouble importing between folders when creating a package with Cython, see this.

Overview

When I was trying to make a package by writing setup.py etc. in Cython Basically, I feel that the folder structure is chaotic. The reason is that when wrapping C ++ with Cython, the part that wraps the implementation on the C ++ side must copy the code of the header on the C ++ side to Cython as well.

This will increase the number of files and you will have to separate the folders. At this time, if you don't understand the meaning of __init__.py or why you put it (for me), you'll get stuck.

What seems to be the best

Since there is a lot of source code, it is good to divide it into various folders, pyxFrom filepxdfilecimportand when, And when `` `pxdfiles are cimport``` across folders You may get an error like "File not found".

To avoid wasting time with such errors I think it would be better to do this.

--C ++ and Cython code are separated by folders --When the folder containing Cython code has subdirectories, put `__init__.py``` in each. --Write all `cimport``` in Cython code with the path seen from the project's parent directory (where setup.py is located).

Folder structure

For example, make a folder structure like this.


setup.py

cpp_code/*

cython_code/
        | -- __init__.py
      folder_a
        | -- __init__.py
        | -- test_a.pyx
        | -- test_a.pxd
      folder_b
        | -- __init__.py
        | --  test_b.pyx
        | --  test_b.pxd
        

In such a case, put __ init__.py in each folder and put it. For example, if test_b.pxd wants to import test_a.pxd```,

test_b.pxd



from cython_code.folder_a.test_a cimport *

If you write it in a unified way like this, you probably won't have to worry too much about import errors.

I summarized it as a memorandum.

end.

Recommended Posts

If you have trouble importing between folders when creating a package with Cython, see this.
Hit Yahoo Answers when you have trouble with blog material
If you get a long error when tabbing an interactive shell with Anaconda
A memo when creating a python environment with miniconda
What to do if you get an Import Error when importing matplotlib with Jupyter
If you can't send an email with python smtplib and have trouble, command line