0

I have written a python package I'm trying to install. With a structure:

packagename 
   |
   |--- setup.py 
   |--- module_name 
            |
            |--- library.c

The library.c file has been successfully installed outside of this package using:

 gcc library.c -Wall -pedantic -o spec_conv -lm -O2

My setup.py file looks like:

from setuptools import setup, Extension

with open("README.md", "r") as fh:
    long_description = fh.read()

module = Extension('library',
                   sources = ['module_name/library.c'],
                   extra_compile_args=['-Wall', '-pedantic', '-o', 'library', '-lm', '-O2'])

    
setup(
    name="module_name", # Replace with your own username
    version="0.0.1",
    author="",
    author_email="",
    description="",
    long_description=long_description,
    long_description_content_type="text/markdown",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
    install_requires=[
        'pandas', 
        'pexpect'],
    #cmdclass={'install': CustomInstall},
    #include_package_data=True,
    ext_modules=[module],     
)

When I run pip install -e . the compile returns an error message: https://pastebin.com/hMLA95G9

Following on from @9769953's comment I've tried to edit the setup.py to directly link to the full path of the file:

from pathlib import Path 

ROOT_PATH = Path(__file__).parent.resolve()

module = Extension('spec_con',
                   sources = ['spec_conv/spec_con.c'],
                   extra_compile_args=['-Wall', '-pedantic', '-o', f'{ROOT_PATH}/module_name/library', '-lm', '-O2'], 
                   library_dirs=["/home/alletro/python_packages"])

But I still get the same error.

4
  • Thank you for your fast response. I've placed the full output into a pastebin. Commented Jul 7, 2022 at 10:34
  • @9769953 That's an interesting point. I've tried to add the path to the file as an f-string (edited to the bottom of the question. I still get the same error. Commented Jul 7, 2022 at 10:38
  • My suggestion would be the other way around: remove the '-o', 'library' items from the extra_compile_args list. Distutils and gcc should already do the right thing. Why do you want to include this option? Commented Jul 7, 2022 at 10:40
  • Removing the '-o' 'library' keywords has fixed the problem. I was including them out of naivety... Commented Jul 7, 2022 at 10:52

1 Answer 1

0

The error is from the linker, that can't find the object file build by the compiler. The object file can't be found, because you specify the object file path manually, and it ends up in the wrong place.

The solution is to remove the '-o', 'library' items from the extra_compile_args option to Extension.

That way, Distutils and gcc will then automatically provide the correct name for the resulting object file (in particular, the correct full directory path), which can then be picked by the linker successfully.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.