The fxcmpy library used in the book Python For Finance doesn't correctly handle its dependencies. This error is compounded by the similar name of the libraries socketio and python-socketio. Here is the solution.

If you are trying to import fxcmpy and get an error about importing socketio

In [1]: import fxcmpy
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-dccc53ad16b2> in <module>
----> 1 import fxcmpy

~/.pyenv/versions/3.8.5/lib/python3.8/site-packages/fxcmpy/__init__.py in <module>
----> 1 from fxcmpy.fxcmpy import fxcmpy as fxcmpy
      2 from fxcmpy.fxcmpy_open_position import fxcmpy_open_position
      3 from fxcmpy.fxcmpy_closed_position import fxcmpy_closed_position
      4 from fxcmpy.fxcmpy_order import fxcmpy_order
      5 from fxcmpy.fxcmpy_oco_order import fxcmpy_oco_order

~/.pyenv/versions/3.8.5/lib/python3.8/site-packages/fxcmpy/fxcmpy.py in <module>
     12 
     13 import requests
---> 14 import socketio
     15 from threading import Thread
     16 import json

ModuleNotFoundError: No module named 'socketio'

You might think you need to install socketio yourself.

$ pip install socketio
Collecting socketio
  Using cached socketio-0.2.1.tar.gz (6.1 kB)
    ERROR: Command errored out with exit status 1:
     command: /home/dennis/.pyenv/versions/3.8.5/bin/python3.8 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-k501njpi/socketio/setup.py'"'"'; __file__='"'"'/tmp/pip-install-k501njpi/socketio/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-cfkvuljn
         cwd: /tmp/pip-install-k501njpi/socketio/
    Complete output (11 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/home/dennis/.pyenv/versions/3.8.5/lib/python3.8/site-packages/setuptools/__init__.py", line 12, in <module>
        from setuptools.extension import Extension
      File "/home/dennis/.pyenv/versions/3.8.5/lib/python3.8/site-packages/setuptools/extension.py", line 7, in <module>
        from setuptools.dist import _get_unpatched
      File "/home/dennis/.pyenv/versions/3.8.5/lib/python3.8/site-packages/setuptools/dist.py", line 16, in <module>
        import pkg_resources
      File "/home/dennis/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pkg_resources.py", line 1479, in <module>
        register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
    AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

But that doesn't work! The problem is that the fxcmpy library doesn't list of all its dependencies so you have to install socketio yourself, *and* there is naming confusion between socketio and python-socketio. The solution is to install the needed library.

pip install python-socketio

And then fxcmpy will work.

$ ipython
Python 3.8.5 (default, Aug 14 2020, 01:11:32) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.17.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import fxcmpy

In [2]: