BaRud
BaRud

Reputation: 3218

import my local module in python3 so it can i installed in the system via pip

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.

Upvotes: 1

Views: 1613

Answers (1)

polarise
polarise

Reputation: 2413

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.

Upvotes: 1

Related Questions