可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
check your $PATH
tox
has a command line mode:
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?
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
回答2:
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)
回答3:
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"
回答4:
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.
回答5:
I stumbled upon this question because I created, successfully built and published a PyPI Package, but couldn't execute it after installation. The $PATH
variable was correctly set.
In my case the problem was that I hadn't set the entry_point
in the setup.py
file:
entry_points = {'console_scripts':
['YOUR_CONSOLE_COMMAND=MODULE_NAME.FILE_NAME:FUNCTION_NAME'],},
回答6:
On Windows, you need to add the path %USERPROFILE%\AppData\Roaming\Python\Scripts
to your path.
回答7:
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/