Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install npm packages in Python virtualenv

There are some npm packages which I would like to install in a Python virtualenv. For example:

  • https://www.npmjs.com/package/pdfjs-dist
  • https://www.npmjs.com/package/jquery-ui

Up to now I only found the complicated way to get these installable in a virtualenv: Create a python package for them.

Is there no simpler way to get npm packages installed in a Python virtualenv?

like image 465
guettli Avatar asked Sep 19 '16 06:09

guettli


People also ask

How do I download NPM packages in Visual Studio?

To open the package manager, from Solution Explorer, right-click the npm node in your project. Next, you can search for npm packages, select one, and install by selecting Install Package.

How do I install global packages?

Install Package Globally NPM installs global packages into /<User>/local/lib/node_modules folder. Apply -g in the install command to install package globally.

How to install NPM in virtualenv?

One solution is to install Node.js/npm inside your virtualenv, so here are the steps to get there: First off, activate your virtualenv, and: That's it! Now you can install npm packages directly to your virtualenv path with npm install -g {package}.

How do I install virtualenv in Python?

Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip. Unix/macOS python3 -m pip install --user virtualenv Windows py -m pip install --user virtualenv Creating a virtual environment¶

How do I install a Python package in a virtual environment?

py -m pip install --user virtualenv Creating a virtual environment ¶ venv (for Python 3) and virtualenv (for Python 2) allow you to manage separate package installations for different projects. They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation.

What are virtual environments in Python and how do they work?

They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation. When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments.


3 Answers

You can install NPM packages on your python virtuaenv using nodeenv.

source ./bin/activate
pip install nodeenv
nodeenv -p

To test if works:

npm install -g npm
npm -v

Sources:

https://pypi.org/project/nodeenv/

https://calvinx.com/2013/07/11/python-virtualenv-with-node-environment-via-nodeenv/

like image 123
Josir Avatar answered Oct 12 '22 23:10

Josir


As @Josir suggests, I have used nodeenv in the past but I had an issue when I wanted to have the node modules inside the venv folder of the project as explained in this question.

In short putting a package.json in venv results in not being able to use npx ... unless it is run from the venv folder whereas putting package.json in venv/lib and running npm install from there results in being able to use npx ... from any folder in the project.

This is due to the NODE_PATH environment variable being set to <myproject>/venv/lib/node_modules.

I created a script to automate this which in substance does:

python -m venv venv
source venv/bin/activate
pip install requirements.txt
cp package.json venv/lib
cd venv/lib
nodeenv -p
npm install --no-optional
like image 27
Jacques Gaudin Avatar answered Oct 13 '22 01:10

Jacques Gaudin


NPM and pip have nothing to do with each other, so you won't be able to install NPM packages inside a virtualenv.

However: NPM installs packages in ./node_modules.

So if you created a virtualenv and installed npm modules inside it

virtualenv myproj
cd myproj
source bin/activate
npm install pdfjs-dist jquery-ui

you will end up with the node packages in myproj/node_modules, which is as close as it gets to "installing NPM inside virtualenv".

like image 11
Nils Werner Avatar answered Oct 13 '22 01:10

Nils Werner