python - Bokeh 服务器应用程序 - ImportError

标签 python python-3.x python-import bokeh

我正在尝试从 main.py 中的 module1.py 和 module2.py 导入函数,如下所示。

# my imports in main.py

from project_dir.bokeh_apps.scripts.module1 import func1
from project_dir.bokeh_apps.scripts.module2 import func2

下面是我的文件夹结构的示例

project/
    project_dir/
        bokeh_apps/
            scripts/
                module1.py
                module2.py
                __init__.py
            main.py
            __init__.py
        dir_1/
        dir_2/
        __init__.py
    dev.ini
    prod.ini

我遇到以下错误

Error running application handler <bokeh.application.handlers.directory.DirectoryHandler object at 0x7fbf647e4208>: No module named 'project_dir.bokeh_apps.scripts'
File "main.py", line 13, in <module>:
from project_dir.bokeh_apps.scripts.module1 import func1 Traceback (most recent call last):
  File "/home/username/.conda/envs/test_env/lib/python3.5/site-packages/bokeh/application/handlers/code_runner.py", line 81, in run
     exec(self._code, module.__dict__)
  File "/var/www/projects/project/project_dir/bokeh_apps/main.py", line 13, in <module>
     from project_dir.bokeh_apps.scripts.module1 import func1
ImportError: No module named 'project_dir.bokeh_apps.scripts'

我正在运行 Bokeh 服务器,如下所示

bokeh serve bokeh_apps/

环境 (我的开发环境和登台环境相同)

Python 3.5
Pyramid 1.5.7
bokeh 0.12.6
tornado 4.5.1

注意: 我在我的开发实例上测试了所有内容,它工作正常,没有问题。当我在我的临时实例上测试它时,我收到这些错误。

如果我做的有什么不对的地方请指正!对此问题的任何帮助将不胜感激:)

最佳答案

这对我组织进口很有帮助。您需要根据您的需求进行调整:

Create Custom Python Package

在 python 应用程序中组织导入的最佳方法是创建一个新包并安装它。该包将与主容器文件夹名称一致。文件夹结构:

funniest/
    funniest/
        __init__.py
    setup.py

__init__.py的内容

def joke():
    return (u'Wenn ist das Nunst\u00fcck git und Slotermeyer? Ja! ... '
            u'Beiherhund das Oder die Flipperwaldt gersput.')

在此主文件夹中创建一个包含以下内容的 setup.py 文件

from setuptools import setup

setup(name='funniest',
      version='0.1',
      description='The funniest joke in the world',
      url='http://github.com/storborg/funniest',
      author='Flying Circus',
      author_email='flyingcircus@example.com',
      license='MIT',
      packages=['funniest'],
      zip_safe=False)

安装包

pip install .               # install the package completely
pip install -e .            # only symbolic link

现在应该可以了

>>> import funniest         # the __init__.py file of this module is executed
>>> print funniest.joke()

关于python - Bokeh 服务器应用程序 - ImportError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52842067/

相关文章:

python - 如何在包中包含示例或测试程序?

python - 地理 View ——墨卡托或 PlateCarree

python - 从具有相同键的多个字典中获取值

python - 不使用 key 从字典中检索信息

python - 从 werkzeug 导入安全导入 werkzeug VS

python - 在焕然一新的 Python 环境中以编程方式从 Python 内部执行 Python 文件

python - 如何在单击按钮时更新 tkinter 笔记本选项卡?

python - PyTorch:如何定义利用迁移学习的新神经网络

python - 一个应该用 Python 写出所有正确括号的程序

python-3.x - 有什么方法可以自定义PyAudio上的录制时间?