Shaun Ziqin Rong Shaun Ziqin Rong - 2 months ago 9
Python Question

Can't import by library name, even though I have python setup.py develop it

I am running into a very strange python import problem. I wrote my own repo, and used a setup.py script to setup the import path, script below:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import unicode_literals

from setuptools import setup, find_packages

__author__ = 'Shaun Rong'
__version__ = '0.1'
__maintainer__ = 'Shaun Rong'
__email__ = 'rongzq08@gmail.com'

if __name__ == "__main__":
setup(name='Quantomic',
version=__version__,
author="Ziqin (Shaun) Rong, Wenxuan Huang",
author_email="rongzq08@mit.edu key01027@mit.edu",
license="MIT License",
packages=find_packages(),
zip_safe=False)


I used
python setup.py develop
to run the codes. Now, however, I can't import the whole library by name Quantomic, any codes like

import Quantomic


or

from Quantomic import settings


will raise the error:
ImportError: No module named Quantomic


I do have a
__init__.py
under the library root, and I checked
sys.path
,
/Users/shaunrong/Documents/projects/Quantomic
is in the path.

I do, however, can import using relative paths in any codes inside Quantomic, using like

import settings


will work OK.

Can anyone tell me what is going on? I am happy to provide more information upon request!

UPDATE

File/Folder structure looks like:

/Quantomic
__init__.py
settings.py
/data
__init__.py
price.py

Answer

Your setup.py file is in the wrong directory. Here is your folder structure

/Quantomic
    setup.py
    __init__.py
    settings.py
    /data
        __init__.py
        price.py 

It should look like this

/Quantomic (can be named anything)
    setup.py
    /Quantomic
        __init__.py
        settings.py
        /data
            __init__.py
            price.py 

When you last ran setup.py, it likely installed a data library into your python installation. Or, because you used develop, it added the path above data to the pythonpath using a pth file in your python libs folder.

Comments