ChatGPT解决这个技术问题 Extra ChatGPT

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

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.

any virtualenv involved?
no, no virtualenv is involved.
when you sudo, you switch to the $PATH of the root user, just make sure his PATH is updated, not your PATH

C
Community

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.


Instead of restarting your shell, you can source ~/.bashrc to reload the .bashrc configuration
Great. I was getting "tox command not found". After updating PATH it works
@chovy did you restart the shell or invoke exec bash?
so i have this exact issue and I am working on cloud compute engine with start up script that does a bunch of things plus pip installs. so what happens is that pip list gives me library and version, yet, which command gives me nothing. I am a bit confused as to where should i put export PATH="$HOME/.local/bin:$PATH" command in my start up script to make it work?
I am having the same issue but the binary is not even installed in /home/USER/.local/bin. Btw.: it was installed there before and I just tried to update and removed it manually before upgrading.
j
joh-mue

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)


The place where pip installs depends on how you installed python, MacOS has multiple ways of installing python, like macports, homebrew and system default. Depending on which version of python is being used, you will have different locations for pip. A easy way to figure out where pip and python are located would be to use "which python" in the terminal and then add the appropriate location to the $PATH.
For macOS 10.14.3 installing Python 3.6 from python.org, which python3 led me eventually (through a link) to /Library/Frameworks/Python.framework/Versions/3.6/bin/.
This answer helped me on Ubuntu 18. I just ran export PATH=$HOME/.local/bin:$PATH on the terminal because I didn't want to permanently change the $PATH
In case of homebrew, I found the pip3 installed binary under /opt/homebrew/bin/. It took me a while because I was trying to find it under /opt/homebrew/lib/python3.9/, but found only site-packages.
J
JL Peyret

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

Thanks JL for the answer, but this is mainly my problem. For my example app rosdep, when I do as you did, here is the output obtained: pip list | grep rosdep rosdep (0.11.4) which rosdep (no output), command line doesn't return anything
which not returning rosdep is consistent with your issue. my point is that you need to find where the rosdep is installed and what $PATH looks like.
Thanks, I had my binaries also being installed in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin. Added that to the PATH and now its working. Thanks, I was having trouble locating the binaries since which wasn't providing any output.
If you can't find the program with which (if it's not in your path yet), you can also try to find the binary using mlocate on linux or mdfind on macOS.
Yep, I'm sorry, but that's totally.... weird (?)... to suggest using "which", because which will only gives the correct answer if the binary CAN actually be found. OP's issue (and most people coming here) are NOT in that case.
W
Willemoes

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.

instead of .bashrc, choose the rc file from your favourite shell.

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.


@Benjamints added an explanation, hope it helps, good luck!
There is a quote to much in the last command. Tried to edit it, but i have to change at least 6 character to save it.
The echo will expand the full PATH because of $PATH in the command! It's uncommon to put the full PATH in bashrc... You need to escape the $ sign!!
A
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 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"

god bless your soul!!! thank you. This should be the accepted answer.
G
Git.Coach

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'],},

z
zb226

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


N
Narendra Rana

On Windows , this helped me https://packaging.python.org/tutorials/installing-packages

On Windows you can find the user base binary directory by running python -m site --user-site and replacing site-packages with Scripts. For example, this could return C:\Users\Username\AppData\Roaming\Python36\site-packages so you would need to set your PATH to include C:\Users\Username\AppData\Roaming\Python36\Scripts. You can set your user PATH permanently in the Control Panel. You may need to log out for the PATH changes to take effect.


mine was 'C:\Users\felip\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Scripts\'. Seriously, wtf?
T
Timo

Windows and Python 3.9 from MS Store

I have a different path with python -m site --user-base and python -m site - yes, the second command without --user-base to get all sites - as the other answers here state:

C:\Users\<your User>\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages

Why is my path different

Because I installed python from MS Store

Solution

Put the above path in your path and replace site-packages with scripts


D
Dharman

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:

Install ansible first using sudo - sudo -H pip3 install ansible 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

run source ~/.profile and restart all terminal shells. Type ansible --version


+1 for your opening statement (just re-run .profile or log out and back in) after using pip3 to install a cmdline python package.
d
d4Rk

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/

D
Diego Velez

had the same issue with macOS Monterey. I had to modify my .bash_profile file and add the following entry

export PATH="~/Library/Python/3.8/bin:$PATH"

The default python version on macOS Monterey is 3.8, but you will have to double check your python version to make sure you're using the correct one


P
Pandy

I solve the problem!

Use pip3 instead pip. pip3 install foobaz vim ~/.zshrc and add:

export PATH="/Users/your_name/Library/Python/3.8/bin:$PATH"

source ~/.zshrc

Now MacOS has shifted the default terminal from bash to zsh. Therefore, you have to source zshrc but not bashrc or bash_profile.

foobaz -v