1

I have a directory structure as:

   tree
    .
    ├── bin
    │   └── mkbib.py
    ├── LICENSE
    ├── mkbib
    │   ├── __init__.py         #empty file
    │   ├── menubar.ui          #a xml file. where should I place it?
    │   ├── menu.py
    │   ├── pybib.py
    │   └── view.py
    ├── mkbib.desktop.in    #should be copied to /usr/local/applications
    ├── README
    └── setup.py

with bin/mkbib.py is the main file, which imports the files in mkbib/. And in bin/mkbib.py, I use:

import mkbib.menu as menu
import mkbib.view as view
# import view
# import pybib

If all files are in same directory, last two lines are enough. I separated them as per the accepted answer here.

But, now, when I am trying to run the code, I am getting error:

 File "mkbib.py", line 26, in __init__
    self.TreeView = view.treeview()
NameError: name 'view' is not defined

My ultimate goal is to install the mkbib app in the /bin/, same as the question I have linked, but I don't have any success.

My setup.py is :

from setuptools import setup, find_packages
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))
with open(path.join(here, 'README'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='mkbib',
    version='0.1',
    description='BibTeX Creator',
    url='https://github.com/rudrab/mkbib',
    author='Rudra Banerjee',
    author_email='[email protected]',
    license='GPLv3',
    packages=['mkbib'],
    package_dir={'mkbib': 'mkbib'},
    scripts=['bin/mkbib.py']
    )

When I run setup.py, I get;

 sudo python3 setup.py develop
running develop
running egg_info
writing top-level names to mkbib.egg-info/top_level.txt
writing mkbib.egg-info/PKG-INFO
writing dependency_links to mkbib.egg-info/dependency_links.txt
reading manifest file 'mkbib.egg-info/SOURCES.txt'
writing manifest file 'mkbib.egg-info/SOURCES.txt'
running build_ext
Creating /usr/lib/python3.5/site-packages/mkbib.egg-link (link to .)
mkbib 0.1 is already the active version in easy-install.pth
Installing mkbib.py script to /usr/bin

Installed /home/rudra/Devel/mkbib/Mkbib
Processing dependencies for mkbib==0.1
Finished processing dependencies for mkbib==0.1

I have also tried exporting pythonpath to the mkbib:

echo $PYTHONPATH
~/Devel/mkbib/Mkbib/mkbib

As I said, if all the files are in same directory, its working flawless.

The mkbib.py's structure is(as asked by GeckStar):

#!/usr/bin/python3

import gi
import sys
# import mkbib
import mkbib.menu as menu
import mkbib.view as view
# import view
# import pybib
import urllib.parse as lurl
import webbrowser
import os
from gi.repository import Gtk, Gio  # , GLib, Gdk
gi.require_version("Gtk", "3.0")


class Window(Gtk.ApplicationWindow):
    def __init__(self, application, giofile=None):
        Gtk.ApplicationWindow.__init__(self,
                                       application=application,
                                       default_width=1000,
                                       default_height=200,
                                       title="mkbib")

        self.TreeView = view.treeview()
        self.MenuElem = menu.MenuManager()
        self.Parser = pybib.parser()
        self.name = ""

.........
class mkbib(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self)
        self.connect("startup", self.startup)
        self.connect("activate", self.activate)
..........

def install_excepthook():
    """ Make sure we exit when an unhandled exception occurs. """
    old_hook = sys.excepthook

    def new_hook(etype, evalue, etb):
        old_hook(etype, evalue, etb)
        while Gtk.main_level():
            Gtk.main_quit()
        sys.exit()
    sys.excepthook = new_hook


if __name__ == "__main__":
    app = mkbib()
    r = app.run(sys.argv)
    sys.exit(r)

Kindly help.

8
  • Have you actually run the setup.py, to install the package? Commented Jul 24, 2016 at 10:58
  • jonrsharpe yes, but mistakenly I have shown the older setup.py with different file structure. Correct (atleast current) setup.py and the run is included now. Kindly have a look. Commented Jul 24, 2016 at 11:05
  • It would be helpful to see the rest of your mkbib.py file, especially the first 26 lines since that's where the error happens. Commented Jul 24, 2016 at 11:44
  • I can't really see why the NameError is being raised. It usually happens when you are using a function or class with an ambiguous name. I'd suggest you to try to import mkbib.view with another name, or change the name of the mkbib folder and see if it works then. Commented Jul 24, 2016 at 13:05
  • @GeckStar: After I build the system, now its giving ImportError for menu and others, if I run from out of Mkbib dir. This is because, the contents of mkbib are placed inside a build/lib, but not placed in /usr/lib/python3.5... Commented Jul 24, 2016 at 13:28

1 Answer 1

1

This is one of Python's quirks: setting up paths to modules and packages. In your case, after you install the mkbib package bin/mkbib.py should simply have:

import mkbib

without any changes to PYTHONPATH. This is because bin/mkbib.py is designed to be used as a binary and assumes that mkbib package is already on the default PYTHONPATH. You can test whether mkbib is on the unmodified PYTHONPATH by running:

$ python -c 'import mkbib'

which should do nothing.

Python takes as reference the current directory . in resolving relative paths, which is why your setup works when all files are in one folder.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think its pythons's problem....if you need to run a executable, you have to place its dependecies in proper place.Your comment is valid, only when the mkbib/ directory is also placed in suitable directory, e.g. /usr/lib64/python3.5/site-packages/, and this is the question: How I can do that.
You need to install the package with python setup.py build && sudo python setup.py install. I assumed you had already done that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.