Category Archives: Uncategorized

How to get python script directory in Spyder?

The script below find the get the script directory in Spyder and set it to current working directory.

The code relies on the internal variable “_ih” and “_i1” spyder uses when running code inside the editor. They stores the cell number and the script filepath to run it in the console.

The if statement can be used to check if “_i1” has been defined to avoid an error running the script outside spyder.

import os
if '_ih' in vars() or '_ih' in globals():
    script_path = _ih[-1].split("'")[1]
    wdir = os.path.dirname(script_path)
    os.chdir(wdir)
elif '_i1' in vars() or '_i1' in globals():
    script_path = _i1.split("'")[1]
    wdir = os.path.dirname(script_path)
    os.chdir(wdir)

How to fix: Moviepy Error “AttributeError: ‘NoneType’ object has no attribute ‘stdout'”

Moviepy is a good library for editing videos on python. But it seems like there are few bugs with the library. One of them is associated with the audioclip.subclip method:

Name of Error: “AttributeError: ‘NoneType’ object has no attribute ‘stdout'”

To fix the error:

Solution 1: Upgrade the moviepy library to version 1.0.3

  • Open terminal
  • Type pip install moviepy –upgrade

Solution2: Update the source code of AudioFileClip.py

  1. Make sure you have moviepy version 1.0.1
  2. go to the moviepy directory:
    cd /usr/local/lib/python3.7/dist-packages/moviepy/
  3. edit the AudioFileClip class at line 93
  4. comment these lines:
    # def __del__(self):
    # self.close()

Reference from: https://github.com/Zulko/moviepy/issues/938

How to fix: “Could not import the PyAudio C module ‘_portaudio'”

When running PyAudio the first time, you may encounter the following error:

Could not import the PyAudio C module ‘_portaudio’.
Traceback (most recent call last):

File “generate_sine.py”, line 7, in
import pyaudio

File “pyaudio.py”, line 116, in
import _portaudio as pa

ImportError: DLL load failed: The specified module could not be found.

It seems that this is caused by failure to install the all the dependencies.

To fix this: install portaudio with the following line:

conda install -c anaconda portaudio

How to fix: League of Legends high ping and ping spikes?

Sometimes, the service provider may route your traffic through an efficient path to league of legend server and that may add additional pings and ping spikes to your game.

To fix this, you can try using a VPN server that is specifically designed to optimize your gaming experience (eg. Express VPN). However, before you reach the VPN server, the traffic is still determined by your internet provider. If using a VPN server still doesn’t work, maybe your best option is to switch to a better internet provider.

How To: Do Conditional OR on Pandas Filters

In pandas, sometimes we may want to have multiple conditional clause on dataframe selection. To do so, use “|” symbol for OR and put the each conditional statement into a curly bracket. Below is a sample code:

import pandas as pd

df = pd.DataFrame({'col_1':[0,1,2,3], 'col_2':[0,0,0,0]})
df.loc[(df['col_1'] > 0) | (df['col_1'] < 3), 'col_2'] = 1

print(df)

#    col_1  col_2
# 0      0      1
# 1      1      1
# 2      2      1
# 3      3      1