Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: All the ways to import user modules from ES6 #296

Open
Filyus opened this issue Oct 25, 2024 · 0 comments
Open

Docs: All the ways to import user modules from ES6 #296

Filyus opened this issue Oct 25, 2024 · 0 comments

Comments

@Filyus
Copy link

Filyus commented Oct 25, 2024

Note

Not tested! Please leave a comment if you have checked any of these methods.

  1. Setting PYTHONPATH in package.json using dotenv-cli:
npm i dotenv-cli

package.json:

{
    "scripts": {
        "dev": "dotenv -v PYTHONPATH=. -- node src/index.js"
    }
}

src/index.js:

const { pymport, proxify } = await import('pymport');

const my_python_module = proxify(pymport('my_python_module'));
  1. Using dynamic import:

Warning

Dynamic imports can make the code more difficult to read and manage because they add asynchronous behavior.

import path from 'node:path';
process.env['PYTHONPATH'] = path.resolve(".");

(async () => {
    const { pymport, proxify } = await import('pymport');
    const my_python_module = proxify(pymport('my_python_module'));
})();
  1. Using require in Node 23.0 (there no need for a --experimental-require-module flag):

Caution

pymport is officially tested only on Node 16.x, 18.x, and 20.x.

Note

You can read more about this in the Joyee Cheung's article and PR.

package.json:

{
  "scripts": {
    "start": "node src/index.js",
  }
}

src/index.js:

import path from 'node:path';
process.env['PYTHONPATH'] = path.resolve(".");
const { pymport, proxify } = require('pymport');

const my_python_module = proxify(pymport('my_python_module'));
@Filyus Filyus changed the title Docs: All the ways to import user modules from ES Docs: All the ways to import user modules from ES6 Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant