Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Testing application built with meson-python#659

Answeredbyeli-schwartz
voidtrance asked this question inQ&A
Discussion options

I am working on an application that is Python-based (so it has a bunch of Python files). However, the application also implements a Python extension and some shared libraries.

The Python files import the Python extension as a module, which, in-turn, opens the shared libraries withdlopen.

I am not clear on how to test my application. When building the extension and libraries, they all get built intobuild/cp311 as described by the documentation. However, when I try to run my application from the Git repo, it fails to import the modules.

I also tried to run the application withPYTHONPATH=build/cp311/ but still it failed to load the modules.

How do I test my application?

Thank you.

You must be logged in to vote

I am not sure what this means. I am using editable builds but do you mean that after the build I would have to copy all of the pure-Python files into the build directorybuild/cp311?

Editable installs will create a hook loader that ensures that any time you import the project, regardless of current directory and regardless of $PYTHONPATH, you get the version you installed via the editable install. It also has some nice tricks such as automatically reloading any time you modify a source file -- because it will then run ninja to rebuild the C extensions before proceeding with the import, so you can edit source files and rerun your code / tests / interactive interpreter without manually re…

Replies: 1 comment 7 replies

Comment options

In order for a python project to be importable when it consists of both .py files and compiled extensions, you have to do one of:

  • have them be different top-level importable modules, no shared namespace
  • install the project (at least in editable mode) and merge the source and build trees together

meson-python supports editable installation, so you can install it that way and then iterate on that for your tests. For a more in-depth approach, you can take a look at:

You must be logged in to vote
7 replies
@voidtrance
Comment options

In order for a python project to be importable when it consists of both .py files and compiled extensions, you have to do one of:

  • have them be different top-level importable modules, no shared namespace

They are separate importable modules. However, the pure-Python module cannot find the C extension. This is the source tree after the mason-python build:

.+- build/|  +- cp311/|     +- ...|     +- src/|        +- ext/|           +- myext.cpython-311-x86_64-linux-gnu.so+- src|  +- ext|     +- myext.c+- module|  +- __init__.py+- main.py

main.py importsmodule needs to importmyext. However,module can't findmyext even withPYTHONPATH set.

  • install the project (at least in editable mode) and merge the source and build trees together

I am not sure what this means. I am using editable builds but do you mean that after the build I would have to copy all of the pure-Python files into the build directorybuild/cp311?

@eli-schwartz
Comment options

I am not sure what this means. I am using editable builds but do you mean that after the build I would have to copy all of the pure-Python files into the build directorybuild/cp311?

Editable installs will create a hook loader that ensures that any time you import the project, regardless of current directory and regardless of $PYTHONPATH, you get the version you installed via the editable install. It also has some nice tricks such as automatically reloading any time you modify a source file -- because it will then run ninja to rebuild the C extensions before proceeding with the import, so you can edit source files and rerun your code / tests / interactive interpreter without manually rebuilding.

Answer selected byvoidtrance
@eli-schwartz
Comment options

If PYTHONPATH contains both:

  • $PWD (to execute the statementimport module)
  • $PWD/build/cp11/src/ext (to execute the statementimport myext)

Then in principle I can't see what the problem would be when running directly from the source tree. Have you tried adding e.g. a debug print to main.py to have it show what it sees as sys.path when the failure occurs?

@orlitzky
Comment options

I think in this example it would work, but if you modify it slightly to:

  • src/mymodule/__init__.py
  • src/mymodule/ext.c

then the issue is that you wind up with two conflicting locations for the same module:

  • src/mymodule (contains the py files)
  • build/src/mymodule (contains the shared libraries)

You can add both to yourPYTHONPATH, but python stops searching once it thinks it knows where the module lives. So, for example,

frommymodule.extimportfoo

will fail because python seessrc/mymodule/__init__.py and decides that "mymodule" lives atsrc/mymodule.

If I drop the src location fromPYTHONPATH, I can import the extension, and if I drop the build location, I can import__init__.py, but getting both to work at the same time is an open problem.

@moi15moi
Comment options

@eli-schwartz Does it means that we cannot run test on a wheel (because i don't think it is possible to install a wheel in editable)?

@eli-schwartz
Comment options

Not completely sure what you mean by that. "Editable installs" per the specification is just a wheel that doesn't contain your code, but instead contains a small import hook that locates your code from your build tree.

It's possible to test a non-editable wheel too. But your tests have to be separately importable from outside the wheel. For example, you can have

myproject/tests/

Or you can have

src/myproject/tests/

But what you cannot do is:

src/myproject/src/myproject/tests/

(which is a bad idea anyway), because the tests cannot be imported in the same python process as the project itself.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
4 participants
@voidtrance@orlitzky@eli-schwartz@moi15moi

[8]ページ先頭

©2009-2025 Movatter.jp