Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError when using console_scripts in setuptools

I am trying to build a program called dnsrep in Python, I am using setuptools so that I can call the dnsrep module without using the command python dnsrep. The setup.py script I wrote is given below:

from setuptools import setup, find_packages

setup(
    name='dnsrep',
    version='0.1',
    description='Program that gives a reputation score to url\'s\n.',
    entry_points = {
        'console_scripts': ['dnsrep = dnsrep:main']
    },
    zip_safe=True,
)

I install the module by using the command:

python setup.py install

My module gets registered but when I run it I get the error:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/bin/dnsrep", line 9, in <module>
    load_entry_point('dnsrep==0.1', 'console_scripts', 'dnsrep')()
  File "build/bdist.macosx-10.6-intel/egg/pkg_resources/__init__.py", line 521, in load_entry_point
  File "build/bdist.macosx-10.6-intel/egg/pkg_resources/__init__.py", line 2632, in load_entry_point
  File "build/bdist.macosx-10.6-intel/egg/pkg_resources/__init__.py", line 2312, in load
  File "build/bdist.macosx-10.6-intel/egg/pkg_resources/__init__.py", line 2318, in resolve
ImportError: No module named dnsrep
like image 759
Akshay Gupta Avatar asked Feb 12 '15 05:02

Akshay Gupta


People also ask

How to fix importerror-no module named Setuptools?

The error message is ImportError: No module named setuptools. The reason for this error is because we do not install the python setuptools module, so to fix it, we just need to install the python setuptools module. 1. Install Python setuptools Module. First, we should download the python setuptools package.

How to fix “Python Setuptools module not installed” error?

The reason for this error is because we do not install the python setuptools module, so to fix it, we just need to install the python setuptools module. 1. Install Python setuptools Module.

What is Setuptools in Python?

Setuptools is a collection of enhancements to the Python distutils that allow developers to more easily build and distribute Python packages, especially ones that have dependencies on other packages. Packages built and distributed using setuptools look to the user like ordinary Python packages based on the distutils.

Does Setuptools automatically call declare_namespace () at runtime?

Setuptools automatically calls declare_namespace () for you at runtime, but future versions may not.


1 Answers

You have to install your python script, before you can call it via your defined entry point

This is my dummy project:

dnsrep/
├── dnsrep.py
└── setup.py

This is how setup.py looks like:

from setuptools import setup
setup(
    name='dnsrep',
    version='0.1',
    description='Program that gives a reputation score to url\'s\n.',
    py_modules=['dnsrep'],
    entry_points = {
        'console_scripts': ['dnsrep = dnsrep:main']
    },
    zip_safe=True,
)

Note the argument py_modules=['dnsrep'], which installs dnsrep.py as a new module.

Finally, this is my dummy implementation of dnsrep.py:

from __future__ import print_function

def main():
    print("Hey, it works!")

After installing, everything works as expected and $ dnsrep prints: Hey, it works!

like image 190
cel Avatar answered Nov 15 '22 01:11

cel