Comments (2)
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.
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)
- stdout/stderr are null? HOT 1
- How to execute py file through node-python-bridge? HOT 8
- python-shell has a broken evaluation parser? HOT 3
- Node.js is broken when python code returns NaN
- python.ex`${src}` is broken HOT 11
- Boolean object property in Python function parameter HOT 1
- ModuleNotFound exceptions HOT 2
- Allow using native promises? HOT 7
- Regex support using rfind()? HOT 3
- Can't import modules HOT 6
- Support for running in worker threads HOT 2
- run a python file ? HOT 2
- Allow to pass python file path as a parameter HOT 5
- EOL error when "\n" in python script HOT 4
- How to keep python bridge connected? HOT 1
- Python-Bridge not working after app is built
- Allow passing arbitrary arguments to the python interpreter
- Python-Bridge not working after create package using pkg npm HOT 1
- 'generator' object has no attribute '__dict__
- Cleanup child process when parent gets killed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-python-bridge.