AKA
AKA

Reputation: 6945

Python Packaging - NameError

I am trying to build a Python package using this tutorial. This is the folder structure:

testpackage\
       testpackage\
              __init__.py
       setup.py
       bin\
          test.sh

setup.py

from setuptools import setup

setup(name='testpackage',
  version='0.1',
  description='Test package',
  url='http://github.com/storborg/testpackage',
  author='ABcd',
  author_email='[email protected]',
  license='Private',
  packages=['testpackage'],
  scripts=['bin/test.sh'],
  zip_safe=False)

__init__.py

from subprocess import call
import shlex
def joke():
    call(shlex.split('bash bin/test.sh testfun'))
    return (u'This is a sample package')

test.sh

#!/bin/bash

testfun()
{
    echo "QQQQQQQQQQQQ"
}

"$@"

I ran the pip install . command from the root testpackage folder, and got installed successfully. Then I accessed python prompt from the same folder and called the joke() function, it got executed and printed the text messages. When I access the python prompt from a different folder and call this function, its throwing a NameError.

>>> import testpackage
>>> testpackage.joke()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/testpackage/__init__.py",       line 2, in joke
call(shlex.split('bash test.sh testfun'))
NameError: global name 'call' is not defined

Why this happening? Any clues?

Upvotes: 1

Views: 166

Answers (1)

AKA
AKA

Reputation: 6945

Finally, I found the solution.

I uninstalled the testpackage using sudo pip uninstall testpackage.

Then I installed it again, sudo pip install .

Whenever I am modifying the package, I uninstall and again install the package. Previously I was trying to reinstall the package without uninstalling it. I thought it will get updated, But it was not.

Tested it, Now working fine.

Upvotes: 1

Related Questions