Thursday, July 23, 2020

How to fix the Library not loaded: @rpath/libmysqlclient.21.dylib error in Django for Mac users



There seems to be some problems with macOS Mojave and Catalina users when trying to connect a Django project to a MySQL database.

ImportError: dlopen(..._mysql.cpython-38-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: .../site-packages/_mysql.cpython-38-darwin.so
  Reason: image not found
NameError: name '_mysql' is not defined

Python seems to be unable to resolve the path to libmysqlclient.21.dylib. One solution is to create a symlink to libmysqlclient.21.dylib in either /usr/local/lib/ or /usr/lib/. You can do so by running this in Terminal:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.21.dylib /usr/local/lib/libmysqlclient.21.dylib

But this doesn't always seem to work. Another solution is to change the path to libmysqlclient.21.dylib specified in _mysql.cpython-38-darwin.so (could be 37 or some other number depending on your Python version). Change it to an absolute path that points to  /usr/local/mysql/lib/libmysqlclient.21.dylib.

_mysql.cpython-38-darwin.so can be found in the MySQLdb directory in site-packages. You can use some Xcode command line tools to fix this. Open a terminal window in the directory that contains _mysql.cpython-38-darwin.so and perform the following:

To check the path:
otool -L _mysql.cpython-38-darwin.so

It's the relative path that seems to be problematic. Command above should output something like this:
_mysql.cpython-38-darwin.so:
    @rpath/libmysqlclient.21.dylib (compatibility version 21.0.0, current version 21.0.0)

Change the relative path to absolute by running this:
install_name_tool -change @rpath/libmysqlclient.21.dylib /usr/local/mysql/lib/libmysqlclient.21.dylib _mysql.cpython-38-darwin.so

Monday, June 15, 2020

Java Swing - How to make a GUI in Java

Hi, Everyone! If you're interested in a Java GUI How To series, I have created a series of videos on YouTube that teaches you the basics of creating a Graphical User Interface (GUI) in Java using the Java Swing (javax.swing) and Abstract Window (java.awt) toolkits. The full playlist can be found here:


Part 1 - Introduction
This is the first video in this Java Swing How To series. In this part, I teach you how to make a JFrame (i.e. a window) using the Java Swing toolkit.

Part 2 - JButtons
In this Java Swing How To video, I teach you how to add a button to your GUI using the JButton class.

Part 3 - Event Handlers
This Java GUI How To video explains the concept of event handlers so that we can make the program do something when the user clicks on the button in our GUI.

Part 4 - Applying the Event Handlers Concepts
In this Java GUI How To video, we implement what we learned about event handlers and make our program print out a message whenever the button in our GUI is clicked.

Part 5 - Introduction to Layout Managers
In this video, I introduce what a layout manager is, which will allow us to set the layout of the components of our GUI. This video also introduces what the contentPane is.

Part 6 - The FlowLayout
In this video, we learn how to implement a simple FlowLayout Layout Manager to help layout the components of java GUI.

Part 7 - Adding another ActionListener for our second button
In this Java GUI How To video, we create another ActionListener object so that we can make our second button also do something when it is clicked on.

Part 8 - The ActionEvent object and the getSource() method
In this part, we will edit the event handler code so that we only use one event listener object for both of our JButton components. In the actionPerformed method, we will make use of if statements and the getSource() method of the ActionEvent object so that the program can figure out which button was clicked and respond accordingly.

Part 9 - Grabbing the contents of a JTextField and setting the text in a JLabel
In this video, we'll learn how to use the getText() and setText() methods. We'll use getText() to retrieve the text typed inside a JTextField, and the we'll place the retrieved text inside a JLabel using the setText() method.

Part 10 - Converting strings to numbers and numbers to strings
In this video, we'll convert values from Strings to numerics and vice versa ( e.g. Double.parseDouble() and Double.toString() ), so that we can perform arithmetic operations on values taken from text fields, and then display the result in labels.

Part 11 - The BorderLayout
In this video, we'll talk about the BorderLayout manager. We'll also add a JTextArea to our GUI. A JTextArea is similar to a JTextField, but it can handle multiple lines.

Part 12 - The GridLayout
In this Java GUI How To video, we'll talk about the GridLayout manager. This allows us to layout our components in a grid (i.e. in rows and columns).

(more videos to be added in the future).