Python equivalent of node.js's npm link to use

2020-07-10 01:21发布

In Node.js, I'm used to using npm link to get a project to use a custom version of a dependency. From the Node documentation:

First, npm link in a package folder will create a globally-installed symbolic link from prefix/package-name to the current folder.

Next, in some other location, npm link package-name will create a symlink from the local node_modules folder to the global symlink.

Is it kosher to do something similar by symlinking into site-packages?

2条回答
老娘就宠你
2楼-- · 2020-07-10 02:08

Perhaps, but what you probably want to do is use virtualenv. Virtualenv allows you to create a python environment isolated from any others:

$ virtualenv myenv
New python executable in myenv/bin/python
Installing setuptools............done.
Installing pip...............done.
$ source myenv/bin/activate

You can then install specific versions of python packages as you please, say version 0.1.0 of a random toolz package I just found, when the lastest version is 0.2.1:

(myenv)$ pip install toolz==0.1.0
Downloading/unpacking toolz==0.1.0
  Downloading toolz-0.1.tar.gz
  Running setup.py egg_info for package toolz
Downloading/unpacking itertoolz>=0.5 (from toolz==0.1.0)
  Downloading itertoolz-0.5.tar.gz
  Running setup.py egg_info for package itertoolz
Downloading/unpacking functoolz>=0.4 (from toolz==0.1.0)
  Downloading functoolz-0.4.tar.gz
  Running setup.py egg_info for package functoolz
Installing collected packages: toolz, itertoolz, functoolz
  Running setup.py install for toolz
  Running setup.py install for itertoolz
  Running setup.py install for functoolz
Successfully installed toolz itertoolz functoolz
Cleaning up...

As you can see it also installs dependencies. You can also generate a requirements file:

(myenv)$ pip freeze
functoolz==0.4
itertoolz==0.5
toolz==0.1
wsgiref==0.1.2

Which you can then use to duplicate those same dependencies in another virtualenv

(myenv)$ pip freeze > reqs.txt
(myenv)$ deactivate
$ source env2/bin/activate
(env2)$ pip freeze
wsgiref==0.1.2
(env2)$ pip install -r reqs.txt 
Downloading/unpacking functoolz==0.4 (from -r reqs.txt (line 1))
  Downloading functoolz-0.4.tar.gz
  Running setup.py egg_info for package functoolz
Downloading/unpacking itertoolz==0.5 (from -r reqs.txt (line 2))
  Downloading itertoolz-0.5.tar.gz
  Running setup.py egg_info for package itertoolz
Downloading/unpacking toolz==0.1 (from -r reqs.txt (line 3))
  Downloading toolz-0.1.tar.gz
  Running setup.py egg_info for package toolz
Requirement already satisfied (use --upgrade to upgrade): wsgiref==0.1.2 in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6 (from -r reqs.txt (line 4))
Installing collected packages: functoolz, itertoolz, toolz
  Running setup.py install for functoolz
  Running setup.py install for itertoolz
  Running setup.py install for toolz
Successfully installed functoolz itertoolz toolz
Cleaning up...
查看更多
Explosion°爆炸
3楼-- · 2020-07-10 02:13

The exact analogue is pip install -e . or python setup.py develop.

https://pip.pypa.io/en/latest/reference/pip_install.html#editable-installs

查看更多
登录 后发表回答