我有一个目录结构为:

   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


其中bin/mkbib.py是主文件,它将导入mkbib/中的文件。在bin/mkbib.py中,我使用:

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


如果所有文件都在同一目录中,那么最后两行就足够了。我按照接受的答案here分离了它们。

但是,现在,当我尝试运行代码时,出现错误:

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


我的最终目标是在mkbib中安装/bin/应用程序,与我链接的问题相同,但没有任何成功。

我的setup.py是:

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']
    )


当我运行setup.py时,我得到了;

 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


我也尝试将pythonpath导出到mkbib

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


就像我说的,如果所有文件都在同一目录中,则它的工作无懈可击。

mkbib.py的结构是(按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)


请帮助。

最佳答案

这是Python的怪癖之一:设置模块和包的路径。对于您而言,在安装mkbib软件包后,bin/mkbib.py应该简单地具有:

import mkbib


无需对PYTHONPATH进行任何更改。这是因为bin/mkbib.py被设计为用作二进制文件,并且假定mkbib程序包已经在默认的PYTHONPATH上。您可以通过运行以下命令来测试mkbib是否在未修改的PYTHONPATH上:

$ python -c 'import mkbib'


这应该什么都不做。

Python在解析相对路径时将当前目录.作为参考,这就是为什么当所有文件都在一个文件夹中时您的设置才能起作用的原因。

07-24 15:04