HOME BLOG

Archive for the ‘Python’ Category

How to run system commands in Python and get the output

Posted on: April 20th, 2020 by Olu No Comments

Hi folks,

Here is an easy way to run system commands in Python and get the output. It is by using subprocess.getoutput command. E.g.

import subprocess
output = subprocess.getoutput("ls -l ~")
print(output)

 

That’s all for now. Till next time, happy software development.

How to fix ‘image not found’ error related to ‘Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib’ when you import psycopg2 on MacOS

Posted on: April 3rd, 2020 by Olu No Comments

Hi folks,

In this post we discuss how to fix an error you may come across when you run import

psycopg2. You may come across this error if you have recently updated openssl to a version greater than 1.0.0 on Mac OS.

The error looks like

 

  File "/Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 737, in dbapi
    import psycopg2
  File "/Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/psycopg2/__init__.py", line 50, in 
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(/Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
  Referenced from: /Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
  Reason: image not found

 

Here is how to fix it:

Activate your project’s virtual environment.

Update psycopg2 using the command:

 

pip install psycopg2 --upgrade

 

If you come across an error like

 

ld: library not found for -lssl

 

then run the following two commands in your terminal

 

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

 

Then, try the aforementioned pip command once more.

That’s it. Once psycopg2 is upgraded, the import issue should be fixed.

How to exit ipdb

Posted on: February 15th, 2020 by Olu No Comments

Hi folks,

If you have done a lot of debugging with ipdb on Python projects in a windows environment using tools like Git bash, you may notice that it’s not very easy to exit ipdb. Pressing familiar keys like Ctrl + c doesn’t work. Here’s how to exit ipdb in Windows:

Run the following command

 

import os; os._exit(0)

 

That’s all for now. Till next time.

How to pip install Python projects from source

Posted on: February 15th, 2020 by Olu No Comments

Hi folks,

In this post I talk about how to pip install Python projects from source.

Let’s say you have a project that you can normally install via pip using a command like pip install someproject, but you want to make some customization to the project.

Here’s how to go about it.

  • First, clone the project from Github.
  • Make any modifications to the project source code as desired.
  • Then change to the project’s root directory.
  • Run the following command to install the project
pip install -e . 
  • If you have the project’s library already installed and you upgrade that version to the latest from your source code using command
pip install -e . -U

 

That’s all for now. Happy software development.