pip成功安装软件包,但在命令行中找不到可执行文件

问题:pip成功安装软件包,但在命令行中找不到可执行文件

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

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

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

有谁知道可能出什么问题了?(下面有更多详细信息)

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

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

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

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

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

对我的$PATH。但是,即使这些程序包可以在python内正常运行,也无法在命令行上找到可执行文件。

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

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

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.

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

I can run: sudo pip install -U rosdep which installs without errors and corresponding files are located in /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"

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

/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.


回答 0

检查您的$ PATH

tox 具有命令行模式:

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

它在哪里?

(编辑:这里的2.7东西没什么大不了的,无论是sub 3.x还是pip的行为都差不多)

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

我的$ 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...

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

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

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

打印出来:

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

现在,将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

输出:

Headers
Python
Resources
bin
include
lib
man
share

check your $PATH

tox has a command line mode:

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

where is it?

(edit: the 2.7 stuff doesn’t matter much here, sub in any 3.x and pip’s behaving pretty much the same way)

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

and what’s in my $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

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

$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, cd to the directory right above lib in the above. Do you see a bin directory? Do you see rosdep in that bin? If so try adding the bin to your $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

回答 1

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

将此添加到您的.bash_profile:

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

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

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

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

Add this to your .bash_profile:

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

That’s where pip installs the executables.

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


回答 2

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

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

通过附加/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"

我使用精确的Python版本来减少当Python将次版本(例如从3.5升级到3.6)升级时可执行文件“消失”的机会。它们将消失,因为如上所示,用户安装路径可能包括Python版本。因此,虽然python3可能指向3.5或3.6,但python3.6始终将指向3.6。在安装其他软件包时,例如pip3.6在over上使用时,请记住这一点pip3

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

# 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"

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 site module. For example, on macOS:

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

By appending /bin to 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:

# 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 python3 could point to 3.5 or 3.6, python3.6 will always point to 3.6. This needs to be kept in mind when installing further packages, e.g. use pip3.6 over pip3.

If you don’t mind the idea of packages disappearing, you can use python2 and python3 instead:

# 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"

回答 3

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

我已经通过pip 安装了pdfx软件包,因此遇到了这个问题中描述的问题。

但是,当我运行它时,什么都没有…

pip list | grep pdfx
pdfx (1.3.0)

然而:

which pdfx
pdfx not found

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

以下是GitHub问题的详细信息: https //github.com/pypa/pip/issues/3813

要解决此问题,只需将添加~/.local/bin$PATH,例如,将以下行添加到.bashrc文件中:

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

在那之后,重新启动您的外壳,一切应该按预期进行。

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

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

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/bin and this is not on the default Debian/Ubuntu $PATH.

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

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

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

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


回答 4

基于其他的答案,对linuxmac您可以运行以下命令:

echo "export PATH=\"`python3 -m site --user-base`/bin:$PATH\"" >> ~/.bashrc
source ~/.bashrc

而不是python3你可以使用任何其他链接到Python版本:pythonpython2.7python3.6python3.9,等。

说明

为了知道用户软件包在当前操作系统(win,mac,linux)中的安装位置,我们运行:

python3 -m site --user-base

我们知道脚本会转到bin/安装软件包的文件夹。

因此,我们将路径连接起来:

echo `python3 -m site --user-base`/bin

然后我们将其导出到环境变量。

export PATH=\"`python3 -m site --user-base`/bin:$PATH\""

最后,为了避免重复执行export命令,我们将其添加到.bashrc文件中,然后运行source以运行新更改,从而为我们提供了开头提到的建议解决方案。

Solution

Based on other answers, for linux and mac you can run the following:

echo "export PATH=\"`python3 -m site --user-base`/bin:$PATH\"" >> ~/.bashrc
source ~/.bashrc

instead of python3 you can use any other link to python version: python, python2.7, python3.6, python3.9, etc.

Explanation

In order to know where the user packages are installed in the current OS (win, mac, linux), we run:

python3 -m site --user-base

We know that the scripts go to the bin/ folder where the packages are installed.

So we concatenate the paths:

echo `python3 -m site --user-base`/bin

Then we export that to an environment variable.

export PATH=\"`python3 -m site --user-base`/bin:$PATH\"

Finally, in order to avoid repeating the export command we add it to our .bashrc file and we run source to run the new changes, giving us the suggested solution mentioned at the beginning.


回答 5

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

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


回答 6

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

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

entry_points = {'console_scripts':

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

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.

In my case the problem was that I hadn’t set the entry_pointin the setup.py file:

entry_points = {'console_scripts':

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

回答 7

除了将python的bin目录添加到$PATH变量之外,我还必须更改该目录的所有者以使其正常运行。不知道为什么我还不是所有者。

chown -R ~/Library/Python/

In addition to adding python’s bin directory to $PATH variable, I also had to change the owner of that directory, to make it work. No idea why I wasn’t the owner already.

chown -R ~/Library/Python/

回答 8

当您使用MacOS安装程序(可从Python网站下载)安装Python或Python3时,它将export~/.profile脚本中添加er 。您只需要做source。重新启动所有终端也可以解决问题。

警告-我认为最好将pip3与Python3配合使用-以获取未来的收益。

如果您已经安装了Python3,则以下步骤可在macOS Mojave上为我工作:

  1. 首先使用sudo– 安装ansiblesudo -H pip3 install ansible

  2. 您创建到Python bin路径的符号链接

sudo ln -s /Library/Frameworks/Python.framework/Versions/Current/bin /Library/Frameworks/Python.framework/current_python_bin

并装订到 .profile

export PATH=$PATH:/Library/Frameworks/Python.framework/current_python_bin

  1. 运行source ~/.profile并重新启动所有终端外壳。

  2. 类型 ansible --version

When you install Python or Python3 using MacOS installer (downloaded from Python website) – it adds an exporter to your ~/.profile script. All you need to do is just source it. Restarting all the terminals should also do the trick.

WARNING – I believe it’s better to just use pip3 with Python3 – for future benefits.

If you already have Python3 installed, the following steps work for me on macOS Mojave:

  1. Install ansible first using sudosudo -H pip3 install ansible

  2. you create a symlink to the Python’s bin path

sudo ln -s /Library/Frameworks/Python.framework/Versions/Current/bin /Library/Frameworks/Python.framework/current_python_bin

and staple it to .profile

export PATH=$PATH:/Library/Frameworks/Python.framework/current_python_bin

  1. run source ~/.profile and restart all terminal shells.

  2. Type ansible --version