Python pip 成功安装软件包,但未从命令行找到可执行文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35898734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:07:18  来源:igfitidea点击:

pip installs packages successfully, but executables not found from command line

pythonmacospipmacports

提问by Sanket_Diwale

I am working on mac OS X Yosemite, version 10.10.3.

我正在使用 mac OS X Yosemite,版本 10.10.3。

I installed python2.7 and pip using macport as done in http://johnlaudun.org/20150512-installing-and-setting-pip-with-macports/

我在http://johnlaudun.org/20150512-installing-and-setting-pip-with-macports/ 中使用 macport 安装了 python2.7 和 pip

I can successfully install packages and import them inside my python environment and python scripts. However any executable associated with a package that can be called from the command line in the terminal are not found.

我可以成功安装包并将它们导入到我的 python 环境和 python 脚本中。但是,找不到与可以从终端中的命令行调用的包相关联的任何可执行文件。

Does anyone know what might be wrong?(More details below)

有谁知道可能有什么问题?(更多详情见下文)

For example while installing a package called "rosdep" as instructed in http://wiki.ros.org/jade/Installation/Source

例如,在按照http://wiki.ros.org/jade/Installation/Source 中的说明安装名为“rosdep”的软件包时

I can run: sudo pip install -U rosdepwhich installs without errors and corresponding files are located in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

我可以运行:sudo pip install -U rosdep安装没有错误并且相应的文件位于/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

However if I try to run : sudo rosdep init, it gives an error : "sudo: rosdep: command not found"

但是,如果我尝试运行 : sudo rosdep init,则会出现错误:"sudo: rosdep: command not found"

This is not a package specific error. I get this for any package installed using pip on my computer. I even tried adding

这不是特定于包的错误。对于在我的计算机上使用 pip 安装的任何软件包,我都会得到这个。我什至尝试添加

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

to my $PATH. But the executables are not found on the command line, even though the packages work perfectly from within python.

到我的$PATH. 但是在命令行上找不到可执行文件,即使这些包在 python 中运行良好。

采纳答案by JL Peyret

check your $PATH

检查你的 $PATH

toxhas a command line mode:

tox有一个命令行模式:

audrey:tests jluc$ pip list | grep tox
tox (2.3.1)

where is it?

它在哪里?

audrey:tests jluc$ which tox
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/tox

and what's in my $PATH?

我的 $PATH 中有什么?

audrey:tests jluc$ echo $PATH
/opt/chefdk/bin:/opt/chefdk/embedded/bin:/opt/local/bin:..../opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin...

Notice the /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin? That's what allows finding my pip-installed stuff

注意/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin吗?这就是允许找到我的 pip 安装的东西的原因

Now, to see where things are from Python, try doing this (substitute rosdepfor tox).

现在,看到那里的东西都是从Python中,尝试这样做(替代rosdeptox)。

$python
>>> import tox
>>> tox.__file__

that prints out:

打印出来:

'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tox/__init__.pyc'

Now, cdto the directory right above libin the above. Do you see a bindirectory? Do you see rosdepin that bin? If so try adding the binto your $PATH.

现在,cd到上面lib的正上方的目录。你看到一个bin目录吗?你看到rosdep那个垃圾桶了吗?如果是这样,请尝试将其添加bin到您的 $PATH。

audrey:2.7 jluc$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7
audrey:2.7 jluc$ ls -1

output:

输出:

Headers
Python
Resources
bin
include
lib
man
share

回答by joh-mue

On macOS with the default python installation you need to add /Users/<you>/Library/Python/2.7/bin/to your $PATH.

在具有默认 python 安装的 macOS 上,您需要添加/Users/<you>/Library/Python/2.7/bin/到 $PATH。

Add this to your .bash_profile:

将此添加到您的 .bash_profile 中:

export PATH="/Users/<you>/Library/Python/2.7/bin:$PATH"

That's where pip installs the executables.

这就是 pip 安装可执行文件的地方。

Tip: For non-default python version which pythonto find the location of your python installation and replace that portion in the path above. (Thanks for the hint Sanket_Diwale)

提示:对于非默认 python 版本which python,找到你的 python 安装位置并替换上面路径中的那部分。(感谢提示 Sanket_Diwale)

回答by Alex Palmer

If you're installing using --user(e.g. pip3.6 install --user tmuxp), it is possible to get the platform-specific user install directory from Python itself using the sitemodule. For example, on macOS:

如果您使用--user(eg pip3.6 install --user tmuxp) 进行安装,则可以使用site模块从 Python 本身获取特定于平台的用户安装目录。例如,在 macOS 上:

$ python2.7 -m site --user-base
/Users/alexp/Library/Python/2.7

By appending /binto this, we now have the path where package executables will be installed. We can dynamically populate the PATH in your shell's rc file based on the output; I'm using bash, but with any luck this is portable:

通过附加/bin到它,我们现在有了安装包可执行文件的路径。我们可以根据输出动态填充 shell 的 rc 文件中的 PATH;我正在使用 bash,但幸运的是,这是可移植的:

# Add Python bin directories to path
python3.6 -m site &> /dev/null && PATH="$PATH:`python3.6 -m site --user-base`/bin"
python2.7 -m site &> /dev/null && PATH="$PATH:`python2.7 -m site --user-base`/bin"

I use the precise Python versions to reduce the chance of the executables just "disappearing" when Python upgrades a minor version, e.g. from 3.5 to 3.6. They'll disappear because, as can be seen above, the user installation path may include the Python version. So while python3could point to 3.5 or 3.6, python3.6will always point to 3.6. This needs to be kept in mind when installing further packages, e.g. use pip3.6over pip3.

我使用精确的 Python 版本来减少当 Python 升级次要版本(例如从 3.5 到 3.6)时可执行文件“消失”的可能性。它们会消失,因为如上所示,用户安装路径可能包含 Python 版本。所以虽然python3可以指向 3.5 或 3.6,但python3.6总是指向 3.6。在安装更多软件包时需要牢记这一点,例如使用pip3.6over pip3

If you don't mind the idea of packages disappearing, you can use python2and python3instead:

如果您不介意包消失的想法,您可以使用python2andpython3代替:

# Add Python bin directories to path
# Note: When Python is upgraded, packages may need to be re-installed
#       or Python versions managed.
python3 -m site &> /dev/null && PATH="$PATH:`python3 -m site --user-base`/bin"
python2 -m site &> /dev/null && PATH="$PATH:`python2 -m site --user-base`/bin"

回答by James Hibbard

I know the question asks about macOS, but here is a solution for Linuxusers who arrive here via Google.

我知道这个问题是关于 macOS 的,但这里有一个针对通过 Google 到达这里的Linux用户的解决方案。

I was having the issue described in this question, having installed the pdfxpackage via pip.

我遇到了这个问题中描述的问题,通过 pip安装了pdfx包。

When I ran it however, nothing...

然而,当我运行它时,什么都没有......

pip list | grep pdfx
pdfx (1.3.0)

Yet:

然而:

which pdfx
pdfx not found

The problem on Linux is that pip install ...drops scripts into ~/.local/binand this is not on the default Debian/Ubuntu $PATH.

Linux 上的问题是pip install ...将脚本放入~/.local/bin默认的 Debian/Ubuntu 中而不是$PATH

Here's a GitHub issue going into more detail: https://github.com/pypa/pip/issues/3813

这是一个更详细的 GitHub 问题:https: //github.com/pypa/pip/issues/3813

To fix, just add ~/.local/binto your $PATH, for example by adding the following line to your .bashrcfile:

要修复,只需添加~/.local/bin到您的$PATH,例如通过将以下行添加到您的.bashrc文件:

export PATH="$HOME/.local/bin:$PATH"

After that, restart your shell and things should work as expected.

之后,重新启动您的 shell,一切都会按预期工作。

回答by Thomas Johannesmeyer

I stumbled upon this question because I created, successfully built and published a PyPI Package, but couldn't execute it after installation. The $PATHvariable was correctly set.

我偶然发现了这个问题,因为我创建、成功构建并发布了一个 PyPI 包,但安装后无法执行它。该$PATH变量被设置正确。

In my case the problem was that I hadn't set the entry_pointin the setup.pyfile:

就我而言,问题是我没有entry_pointsetup.py文件中设置:

entry_points = {'console_scripts':

['YOUR_CONSOLE_COMMAND=MODULE_NAME.FILE_NAME:FUNCTION_NAME'],},

回答by Jared Beach

On Windows, you need to add the path %USERPROFILE%\AppData\Roaming\Python\Scriptsto your path.

在 Windows 上,您需要将路径添加%USERPROFILE%\AppData\Roaming\Python\Scripts到您的路径中。

回答by d4Rk

In addition to adding python's bindirectory to $PATHvariable, I also had to change the owner of that directory, to make it work. No idea why I wasn't the owner already.

除了将 python 的bin目录添加到$PATH变量之外,我还必须更改该目录的所有者才能使其工作。不知道为什么我已经不是所有者了。

chown -R ~/Library/Python/