I want to make python package with C extensions. I want this to be done with cython. My structure is:
.
├── build
│ ├── lib.linux-i686-2.7
│ │ └── pyA13SOM
│ │ ├── cython
│ │ │ └── spi.so
│ │ └── __init__.py
│ └── temp.linux-i686-2.7
│ └── pyA13SOM
│ └── cython
│ ├── my_test.o
│ └── spi.o
├── CHANGES.txt
├── Makefile
├── MANIFEST
├── pyA13SOM
│ ├── cython
│ │ ├── clibraries
│ │ │ └── spi_test.c
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── spi.c
│ │ ├── spi.pyx
│ │ └── spi.so
│ ├── gpio
│ │ ├── gpio.c
│ │ ├── gpio_lib.c
│ │ ├── gpio_lib.h
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── i2c
│ │ ├── i2c.c
│ │ ├── i2c_lib.c
│ │ ├── i2c_lib.h
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── spi
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── spi.c
│ │ ├── spi_lib.c
│ │ └── spi_lib.h
│ └── utilities
│ └── color.h
├── README.txt
└── setup.py
My setup file is:
from distutils.core import setup
from distutils.core import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
module_gpio = Extension('pyA13SOM.gpio',
sources=['pyA13SOM/gpio/gpio_lib.c', 'pyA13SOM/gpio/gpio.c'])
module_i2c = Extension('pyA13SOM.i2c',
sources=['pyA13SOM/i2c/i2c_lib.c', 'pyA13SOM/i2c/i2c.c'])
module_spi = Extension('pyA13SOM.spi',
define_macros=[('CYTHON_IN_USE', '1')],
sources=['pyA13SOM/spi/spi_lib.c', 'pyA13SOM/spi/spi.c'])
setup(
name='pyA13SOM',
version='0.2.0',
packages=['pyA13SOM'],
# ext_modules=[module_gpio, module_i2c, module_spi],
cmdclass={'build_ext': build_ext},
ext_modules=cythonize("pyA13SOM/cython/*.pyx"),
)
The tree is in ~/mydir/
. I go to ~/mydir/
and do: python setup.py install
.
Everything in the build process is OK. Next I try to test import
. When I import pyA13SOM.cython.spi
, it should give me "Hello world" message. And it does.
~/mydir/$ **python -c "import pyA13SOM.cython.spi"**
Test:
Hellowwwwwwwwwww!
But when I do this from another directory:
~/someotherdir/$ **python -c "import pyA13SOM.cython.spi"**
ImportError: No module named cython.spi
Any idea why does this happen?
You might need to include the directory in which your newly built .spi file is located into your $PYTHONPATH. Otherwise python cannot find the file to import it. While you are in ~/mydir/, python searches the local path if I am not mistaken...
Depending on the shell you are using, you can include the ~/mydir/ directory into the pythonpath with the following:
for the bash and sh shells:
PYTHONPATH=$PYTHONPATH:~/mydir/
export $PYTHONPATH
for the csh/tcsh environment:
set PYTHONPATH = ($PYTHONPATH ~/mydir/)
These two commands add the ~/mydir/ temporarily to your $PYTHONPATH. If you want to add the path permanently, you will have to add the above commands to your ~/.bashrc or ~/.tcshrc, respectively.
Hope this helps...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.