solvcon / modmesh

Toolkit for solving partial differential equations
BSD 3-Clause "New" or "Revised" License
38 stars 38 forks source link

Enhance the way for importing python third-party library #378

Closed j8xixo12 closed 2 months ago

j8xixo12 commented 3 months ago

In #364, it created a module called pylibmgr to manage and import python third-party library from third-party library root thirdparty.

However, the way for import third-party library is appending to sys.path, which deteriorate maintainability. Reference: https://github.com/solvcon/modmesh/pull/364#discussion_r1650208176

modmesh needs a better way to import third-party library!

yungyuc commented 3 months ago

Thanks, @j8xixo12 , for creating this issue to track the work to enhance the path finding.

However, the way for import third-party library is appending to sys.path, which deteriorate maintainability. Reference: #364 (comment)

Yes, the key is that we should not modify sys.path (or adding/deleting entries) inside the modmesh package. It will surprise other packages.

yungyuc commented 2 months ago

@j8xixo12 is there work remaining here for the issue?

j8xixo12 commented 2 months ago

Yes, there is some remaining for this issue.

I found a way in the official Python documentation that allows Python to load a module or package from a source file with a global scope.

https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly

I think this is a good way to import third-party library without modified sys.path!

yungyuc commented 2 months ago

Yes, there is some remaining for this issue.

I found a way in the official Python documentation that allows Python to load a module or package from a source file with a global scope.

https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly

I think this is a good way to import third-party library without modified sys.path!

If you are OK with using the Python magic, perhaps meta_path can be considered: https://yyc.solvcon.net/en/latest/nsd/12advpy/advpy.html#module-magic-with-meta-path . meta_path gives me an impression that it provides higher modularity for the third-party import.

Both ways are OK to me. What do you think?

j8xixo12 commented 2 months ago

Yes, there is some remaining for this issue. I found a way in the official Python documentation that allows Python to load a module or package from a source file with a global scope. https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly I think this is a good way to import third-party library without modified sys.path!

If you are OK with using the Python magic, perhaps meta_path can be considered: https://yyc.solvcon.net/en/latest/nsd/12advpy/advpy.html#module-magic-with-meta-path . meta_path gives me an impression that it provides higher modularity for the third-party import.

Both ways are OK to me. What do you think?

Using meta_path looks good to me, let me use it to enhance the importing way of third-party library.

yungyuc commented 2 months ago

@j8xixo12 Do we have remaining work after merging PR #390?

j8xixo12 commented 2 months ago

No, there is no remaining work, let’s close this issue.