Giter Site home page Giter Site logo

Comments (2)

munro avatar munro commented on June 27, 2024 1

All you should have to do to run the bridge in a virtualenv is pass in {python: venv_python_path, env: venv_env_variables}β€”and it should be good to go!

So this would work really well as a separate module, and I think you could get away with a very light wrapper around virtualenv so that you don't have to write too much glue code.

import { PythonVirtualEnv } from 'python-virtualenv'
import { PythonBridge } from 'python-bridge'
import { promisify } from 'utils';
import { join as path_join } from 'path';

declare interface PythonVirtualOptionsBase {
    python: string // python intepreter to use in env
    args: string // extra arguments to pass to virtualenv
}

declare interface PythonVirtualOptionsPath extends PythonVirtualOptionsBase {
    path: string, // path to virtualenv
}

declare interface PythonVirtualOptionsNamed extends PythonVirtualOptionsBase {
    name: string, // name of virtualenv, creates in ~/.virtualenvs/
    virtualenv_path: string | undefined, // override for ~/.virtualenvs
}

declare class PythonVirtualEnv {
    python: string; // Python path
    env?: { [key: string]: string | undefined; };
    created: boolean; // If it created the virtualenv on initializing

    constructor(PythonVirtualOptionsPath | PythonVirtualOptionsNamed)  // creates new virtualenv if doesn't exist
    unlink(): void; // delete virtualenv

    // child_process API, but everything runs in the virtual env
    exec(command[, options][, callback]);
    execFile(file[, args][, options][, callback]);
    fork(modulePath[, args][, options]);
    spawn(command[, args][, options]);
}

async function example() {
    const virtualenv_path = path_join(__dirname, '.virtualenv')
    const requirements_path = path_join(__dirname, 'requirements.txt');
    const virtualenv = new PythonVirtualEnv({python: 'python3', path: virtualenv_path});
    const exec = promisify(utils.virtualenv.exec);
    await exec('pip install pip==9.0.1');  // skips if installed
    await exec(`pip install -r ${requirements_path}`);  // skips if installed

    // run python bridge inside virtualenv
    const python = new PythonBridge({python: virtualenv.python, env: virtualenv.env});

    // install tensorflow
    if (!await python`'tensorflow' in sys.modules`) {
        await virtualenv.exec('pip', ['install', 'tensorflow'])
    }
}

from node-python-bridge.

huan avatar huan commented on June 27, 2024

The python-virtualenv seems is what I want.

Will play with the code later. I believe it worth to be put in FAQ.

Thanks!

from node-python-bridge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.