0

On the new MacOS 13 M1/M2, python 3.9 is pre-installed with pip3 (no pip). I don't install other Python versions with brew/macport or Python installer, only the pre-install 3.9.

With the pre-install python, the global site-package is located in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages (other installers will be /Library/Frameworks/Python.framework/Versions/3.9/)

The per-user site-package is located in ~/Library/Python/3.9/lib/python/site-packages

When I pip3 install a package, it will be installed to the per-user location, not the global location, along with the executable files installed ~/Library/Python/3.9/bin. For example, after I upgrade pip3, pip will also be installed in ~/Library/Python/3.9/bin.

I want to run these executable files installed by pip3 without specifying the full path. When I upgraded pip3 the system still complained it couldn't find pip, and it took me some time to figure out why.

I can add ~/Library/Python/3.9/bin to PATH directly but I feel it is an ad hoc solution. For example, with the next macOS release, the pre-installed Python may upgrade to 3.10 (as that happened before) I may need to add ~/Library/Python/3.10/bin to PATH too. I have seen from time to time that PATH is modified to be long and ugly just to include Python and the related executable files.

Should I set PYTHONPATH to ~/Library/Python/3.9/bin and add PYTHONPATH to PATH? I would like to know what is the best practice for using PYTHONPATH/PYTHONHOME for the pre-install python on Mac.

Both PYTHONPATH/PYTHONHOME are not set by default.

From What exactly should be set in PYTHONPATH?, the agreed-upon answers are that I don't need to set PYTHONPATH unless there are non-standard libraries I want to use. But these answers seem not to solve my problem, of course, I need to use non-standard libraries for my development, say pytorch.

--- update ---

From the comments I got, I realized I installed python3 with xcode-select --install, MacOS 13 doesn't preinstall python3.

Also from the comments let me reiterate my questions, 1. Should I set PYTHONPATH/PYTHONHOME, why or why not? 2. I feel adding ~/Library/Python/3.9/bin to PATH is an ad hoc solution and I would like to know if is there a better way.

Of course, I need to add those executables' paths to the PATH in the end. But I didn't know the correct usage of PYTHONPATH so I thought maybe I could add those executables' paths to PYTHONPATH and then add PYTHONPATH to PATH. In that way when I have more paths to add, I just need to edit PYTHONPATH and PATH remains the same.

But that seems not to be the proper usage of PYTHONPATH from what I have so far.

9
  • Please explain the exact issue you are having. macOS does not include python3 so how did you install it (Xcode does include it) From Xcode pip3 is on the PATH. What application are you tryingn to install
    – mmmmmm
    Oct 7, 2023 at 10:27
  • As I said in my question "My goal is to run these executable files installed by pip3 without specifying the full path.", e.g. pip install python-dotenv will install dotenv in ~/Library/Python/3.9/bin Oct 7, 2023 at 10:30
  • @mmmmmm I updated my question according to your comment. Oct 8, 2023 at 3:37
  • FWIW I am not sure how much it matches your concerns but a lot of “advanced” pip users seem to recommend python -m pip install python-dotenv to avoid the pip being outta sync w the python. I’ve mostly configured pip, somehow, now, so it’s not worth doing for me. But I encountered glitches that python -m pip would have avoided. I would also avoid messing w a system macos python, install your own python.org download or macports. Homebrew installs can auto-update python versions, supposedly which can cause issues.
    – JL Peyret
    Oct 8, 2023 at 7:09
  • @JLPeyret thanks for the comment but I have yet to find a way to set my path. Of course I can just add ~/Library/Python/3.9/bin to my path, but that kinda defeat the purpose of me asking this long question. Oct 8, 2023 at 7:33

1 Answer 1

3

Leave PYTHONHOME and PYTHONPATH alone.

Add the bin directory containing the executables you want to run to your path.

11
  • Why leave PYTHONHOME and PYTHONPATH alone? Oct 8, 2023 at 1:49
  • I check many articles about setting the path right for python, but I didn't any of them say adding ~/Library/Python/3.9/bin to path. Of course, I can just do that. But I want to know if there is a better way. Oct 8, 2023 at 2:03
  • It is the simplest and only way for the executables otherwise the shell can't find them. Python is setup so that the PYTHON* variables just work.
    – mmmmmm
    Oct 8, 2023 at 9:53
  • If you are just running python3 then that is on the PATH and you do not need anything else.
    – mmmmmm
    Oct 8, 2023 at 9:56
  • 1
    @Qiulang邱朗 Do you think there's some other way for the shell to find an executable? Oct 8, 2023 at 14:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .