Reinstall python packages after homebrew python#3.8 update - python-3.x

I have recently updated python to python#3.8 via homebrew.
I then noticed that my python scripts were unable to link to the global python packages that I had previously installed with pip (packages installed globally and not within a virtual environment).
This has sent me down the rabbit hole of understanding how python is set up on macos and I have several questions.
pip installed by homebrew will now install all packages in /usr/local/lib/python3.8/site-packages.
Does that mean I need to reinstall all my python packages that used to live (and still are) in /usr/local/lib/python3.7? Should I remove the latter directory after? (For that matter I can see I have also a /usr/local/lib/python3.6 directory)
What's the efficient way of managing this in the future when the next python update comes along? Virtual envs per project (I'm not very familiar with the use of virtual envs)?
When I run
brew list | grep python I get the following list
python
python3
python#3.8
I can see python#3.8 is correctly symlinked as /usr/local/bin/python3 but I don't know what the python and python3 in the above list are since looking at /usr/local/Cellar I can see that I have an empty /usr/local/Cellar/python directory and a /usr/local/Cellar/python3 directory that is symlinked to it. Can I juste remove these two folders?
Finally, looking into all this, I was surprised to discover a python3 executable living in /usr/bin.
Looking into the sys.path when I execute /usr/bin/python3, I can see it's some sort of python installed by Xcode since it's looking for libraries in various Xcode.app directories.
Since my /usr/local/bin is at the top of my PATH envt variable, this python3 will never be called but I wonder why I'd need it at all and whether I can just get rid of it.
I suppose the answer to a lot of these questions is to use virtual environments, which I've miraculously avoided doing for so long... Would love some help understanding the above though.
EDIT: Running brew info python or brew info python3 returns the same thing
python#3.8: stable 3.8.3 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python#3.8/3.8.3_2 (4,268 files, 65.5MB) *
Poured from bottle on 2020-07-12 at 23:19:08
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/python#3.8.rb

I had a similar issue with a system running Python 3.9
I had initially not run the final command (only the --dry-run), as everything seemed fine, but as suggested during the installation, using brew link to clobber and create new symlinks was needed.
% brew link --overwrite --dry-run python#3.9
Would remove:
% brew link --overwrite python#3.9
Linking /usr/local/Cellar/python#3.9/3.9.1_1... 23 symlinks created
%
As suggested by MisterMiyagi in the comments, this was uncovered via brew doctor
...
Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
python#3.9

Related

Mac install python without developer tools

I am trying to install python 3.11 without the Mac developer tools, which will give me python3.9. But when I used the vscode to open a python file, the command line installer poped up and showed the following message 'the python3 command requires the command line developer tools ....'. But I can run the python3 command in the terminal successfully.
The annoying part is the pop up window. I found some suggestions about relink or rebuild the Xcode / command line tools. But in my case, there is no Xcode path. Please let me know if there is a way to configure the path correctly.
Thank you,
I checked the link of python3 in /usr/bin
I install the python directly from the http://python.org
Do you use Homebrew in your terminal?
You can try to run brew link --overwrite --dry-run python#3.11 in your terminal to see if you need to do an update for your env path. If you do need it, the terminal will give you suggestions to relink, ex. brew link --overwrite python#3.11 or rm '/usr/local/bin/2to3-3.11'
I also had an issue when installing python3.11 through Homebrew in my terminal, it said I must use xcode-select --install to install python3.11.
So later on, I followed this video step by step, and now it works. Whenever I check python3 --version, it returns Python 3.11.1. It also allows me to install another library that depends on python3.11.
Maybe it is worth checking the current version in your terminal and seeing if need to install again the package.

gobject-2.0-0 not able to load on macbook

I am facing this error when I start my flask application on Python3 and Mac OS:
OSError: cannot load library 'gobject-2.0-0': dlopen(gobject-2.0-0, 2): image not found. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'
I am using weasyprint in my project which is causing this issue.
I tried to install glib and it is installed in my system.
I just managed the same issue on my Mac M1.
The problem was that symlinks to the libraries were not created (can't say should it be done by homebrew or weasyprint when installation).
So, I had to do it manually
sudo ln -s /opt/homebrew/opt/glib/lib/libgobject-2.0.0.dylib /usr/local/lib/gobject-2.0
sudo ln -s /opt/homebrew/opt/pango/lib/libpango-1.0.dylib /usr/local/lib/pango-1.0
sudo ln -s /opt/homebrew/opt/harfbuzz/lib/libharfbuzz.dylib /usr/local/lib/harfbuzz
sudo ln -s /opt/homebrew/opt/fontconfig/lib/libfontconfig.1.dylib /usr/local/lib/fontconfig-1
sudo ln -s /opt/homebrew/opt/pango/lib/libpangoft2-1.0.dylib /usr/local/lib/pangoft2-1.0
This solved the problem.
In order to TEST if Python can find the library you may run
from ctypes.util import find_library
find_library('gobject-2.0') # Pass any other lib name as an argument
UPD. There is no such problem if you install python with homebrew.
If you installed weasyprint package in your virtual env but did not installed some required packages for that, then that might be the reason. It was actually, in my case.
If you're mac user you need install pango and libffi, not to mention python as well.
Installation guide on mac
brew install python pango libffi
(venv) pip install weasyprint
To expand on the existing answers on Apple Silicon (M1) Macs:
If you have installed the packages with Homebrew and they are still not found or linked under /usr/local/lib, it is because they are installed on arm64 and found in /opt/homebrew/lib instead.
If you're using Python installed with Homebrew it should work without any extra work, however system Python and any managed Python versions (e.g. installed with Pyenv) will require some configuration.
1. Manual symlinking
Instead of linking each library individually to /usr/local/lib, you can link the /opt/homebrew/lib contents (as long as you don't have an existing /usr/local/lib directory):
sudo ln -s /opt/homebrew/lib /usr/local/lib
This will work as long as the library you're looking for is not from a keg-only formula (those will have to be linked individually).
2. Environment variables
A lot of answers point to setting some environment variable, like LDFLAGS or DYLD_LIBRARY_PATH to add search paths for libraries, but these will not work with Python based on my testing:
macOS comes with System Integrity Protection (SIP) which, among other things, sanitizes your environment variables in subprocesses, for example Python. Anything starting with LD or DYLD will be purged, so setting the environment variables in your terminal profile will not work.
You can Disable SIP to get these working, but Apple recommends only doing it temporarily when needed.
If you decide to go this route, here are a few options:
In Homebrew's Github discussions the question was answered by setting LDFLAGS:
export LDFLAGS=-L/opt/homebrew/lib
Similarly you could add the necessary paths to DYLD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=/opt/homebrew/lib
I had the same issue after the homebrew update. Turned out the issue was because of the older pango lib version.
I did brew install pango
This upgraded pango lib from 1.48.2 -> 1.50.4 which internally installed gobject's latest version as dep. And my issue got resolved.

How do I restore my Haskell setup to the bare Homebrew configuration?

I have a system setup of Haskell that I've maintained with Homebrew and subsequent cabal install invocations for various packages. I would like to take this back to the bare installation created with brew install haskell-stack.
How do I do this? Right now I seem to have a bunch of stuff lying around (e.g. old docsets, executables like ghc-mod etc.) at the system level, even after deleting all installed packages with rm -r ~/.ghc. Is there a reliable way to get back to the basic configuration that brew install haskell-stack creates?

Installing Haskell with Homebrew fails

I had installed Haskell before, but that was hopelessly outdated. I tried to uninstall everything based on the documents I found and reinstalled it using homebrew.
brew install ghc cabal-install
This succeeded
==> Downloading https://homebrew.bintray.com/bottles/ghc-7.10.1_1.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring ghc-7.10.1_1.yosemite.bottle.tar.gz
🍺 /usr/local/Cellar/ghc/7.10.1_1: 5423 files, 821M
==> Downloading https://homebrew.bintray.com/bottles/cabal-install-1.22.2.0.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring cabal-install-1.22.2.0.yosemite.bottle.1.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
==> Summary
🍺 /usr/local/Cellar/cabal-install/1.22.2.0: 6 files, 19M
But then I get this:
> ghci
-bash: /usr/bin/ghci: No such file or directory
And
> cabal
-bash: /usr/bin/cabal: No such file or directory
Since I can run other packages that are in my Cellar, I would think that I should be able to run these packages as well?
I think you have some lingering symlinks or similar in /usr/bin. Homebrew automatically symlinks binaries from its "cellar" to /usr/local/bin (notice that the directory includes local).
A reasonable course of action for you might be
Check if Homebrew installed ghc, cabal, etc in /usr/local/bin (they should be symlinks pointing to somewhere under /usr/local/Cellar/). If they do exist you can conclude that Homebrew did not screw up.
Check what kind of thing /usr/bin/ghci, /usr/bin/cabal actually are: eg ls -l /usr/bin/ghci. It will probably tell you that they are symlinks which point to files that don't exist, they are probably left from your previous Haskell installation.
Remove the broken symlinks and make sure that /usr/local/bin is somewhere on your $PATH.
(Now there have been people who do not recommend installing GHC using Homebrew, you might want to try the installation instructions found on the Haskell homepage: https://www.haskell.org/downloads. On the other hand I don't see anything too strange in Homebrew's GHC formula, so it might be fine currently.)

Installing additional packages for Cygwin

To install additional packages for Cygwin, do I just need to run the setup.exe again and choose from the packages list?
Also, doing this won't harm my computer in terms of 2 Cygwin instances being installed or problems of that kind (I'm kind of a noobie with these things).
Last, there is no package manager in Cygwin which you can run in the command line? Something similar to Pip in Python.
No, adding additional packages doesn’t modify the current settings. There is a
package manager called apt-cyg which installes additional packages from
command-line. To install apt-cyg follow the below steps:
wget rawgit.com/transcode-open/apt-cyg/master/apt-cyg
install apt-cyg /bin
Note: wget should be installed for downloading the apt-cyg. To Use apt-cyg for
installing additional package (after following the above steps):
apt-cyg install ncurses
No, it doesn't hurt the current setup. The install program knows what's installed already.
Having said that, I long ago got into the habit of installing all of Cygwin since, despite its size, it's still minuscule compared to the size of modern hard disks. That way, you won't ever have to worry about whether a package is installed or not.
Re-run the setup executable like "cygwin_setup-x86_64.exe" should do it.
"Install from Internet"
Accept your existing root directory (from your existing installation)
Use your existing "Local Package Directory"
On the screen, view "Full"
Search for the new package you want to add
Go through the installation
Additional option, may be helpful for someone:
To install additional packages in windows from windows command line you can use your cygwin installer.
I suppose, you've already downloaded it to install cygwin from here https://cygwin.com/install.html.
$ setup-x86_64.exe -q -P graphviz
see this guide for details:
http://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/
There is no package management in Cygwin outside of the setup program. The setup only applies updates to your current installation, it does not overwrite packages than what you already have.
So if you want new packages just rerun the setup program to install packages.
You can just look for the package binaries and decompress them in the C:\cygwin\bin folder.
I did that for dos2unix ( https://cygwin.com/packages/summary/dos2unix.html ) and trying it out now.

Resources