2

I am trying to learn the boost python API to create my own python(3.6) module. I am able to successfully compile the following c++ code and get the resulting pythonTest.dll that I am generating.

#define BOOST_ALL_DYN_LINK
#include <boost/python.hpp>
#include <string>

const std::string hello_world(void)
{
    return std::string("hello world!\n");
}

BOOST_PYTHON_MODULE(pythonTest)
{
    namespace python = boost::python;

    python::def("hello_world", hello_world);
}

I then copy the pythonTest.dll to my module folder that looks like the following. (I rename the pythonTest.dll to pythonTest.pyd)

pythonTest\
    __init__.py
    pythonTest.pyd

When I execute python, it will import the module but it does not find my hello_world function:

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pythonTest
>>> pythonTest.hello_world()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
**AttributeError: module 'pythonTest' has no attribute 'hello_world'**

>>> help(pythonTest)
Help on package pythonTest:

NAME
    pythonTest

PACKAGE CONTENTS
    pythonTest

FILE
    <...>\pythontest\__init__.py

>>> dir(pythonTest)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__','__name__', '__package__', '__path__', '__spec__']
>>>

What am I missing? Thanks.

EDIT: This is the output from the visual studio build:

1>------ Rebuild All started: Project: pythonTest, Configuration: Release x64 ------
1>Build started 7/10/2017 2:49:53 PM.
1>InitializeBuildStatus:
1>  Creating "x64\Release\pythonTest.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  pythonTest.cpp
1>G:\DataBackup\dev-vc13\3rdParty\boost_1_64_0\boost/python/detail/caller.hpp(55): warning C4244: 'return' : conversion from 'Py_ssize_t' to 'unsigned int', possible loss of data
1>Link:
1>     Creating library G:\DataBackup\dev-vc13\temp\pythonTest\x64\Release\pythonTest.lib and object G:\DataBackup\dev-vc13\temp\pythonTest\x64\Release\pythonTest.exp
1>  Generating code
1>  Finished generating code
1>  pythonTest.vcxproj -> G:\DataBackup\dev-vc13\temp\pythonTest\x64\Release\pythonTest.dll
1>PostBuildEvent:
1>          1 file(s) copied.
1>FinalizeBuildStatus:
1>  Deleting file "x64\Release\pythonTest.tlog\unsuccessfulbuild".
1>  Touching "x64\Release\pythonTest.tlog\pythonTest.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:04.21
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
2
  • Does Visual Studio compiler show any warnings while building your DLL? Commented Jul 6, 2017 at 15:20
  • Write from PythonTest import * in __init__.py Commented Oct 17, 2020 at 21:35

1 Answer 1

1

try using the dir() function

>>> dir(pythonTest)

that will show u the available methods and functions of packages

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.