简体   繁体   English

Python无法找到我的包:

[英]Python can't find my packages:

I have been able to run a web app I wrote in bottle from my IDE. 我能够运行我在IDE中用瓶子写的网络应用程序。 It works well, and when I tried to move it to a new system, it broke (couldn't find the files I wrote in the lib folder). 它工作得很好,当我试图将它移动到一个新系统时,它崩溃了(找不到我在lib文件夹中写的文件)。

How do I get it to find my lib files? 如何让它找到我的lib文件? I found this for bottle: 我发现这个用于瓶子:

bottle.TEMPLATE_PATH.insert(0,'/absolut/path/to/templates/')

And I think that will help with the missing .tpl files, but how do I do this for python in general? 我认为这将有助于丢失.tpl文件,但我如何为python做这一般? Can I add some sort of python.PATH.insert() to my main.py? 我可以将一些python.PATH.insert()添加到我的main.py中吗?

Here is my dir structure: 这是我的目录结构:

DEV
├───.idea
│   ├───inspectionProfiles
│   └───scopes
└───myProject *(Also has a .py file I need)*
    ├───output
    │   └───processed
    └───webapp
        ├───src
        │   ├───lib
        │   │   └───*(All my .py files I need)*
        │   ├───static
        │   │   ├───css
        │   │   ├───files
        │   │   ├───img
        │   │   └───js
        │   └───views *(All the .tpl files I need)*
        │       ├───main
        │       ├───popup
        │       └───reports
        └───main.py *(The file that acesses the .py files)*

Relevant Code: 相关守则:

import threading
import time
import datetime

import crud_ops
from helper_functions import load_config_file, dt_ona_sum_format
from server import run_bottle
from myProject import ona_sum_tool #THIS LINE HERE

 ...


def run_queue(col):

    while(1):
        if not col:
            print "Unable to connect to db."
        else:
            my_limit = 10
            processing_cursor = col.queue.find({"status":"processing"}).limit(my_limit)
            if not processing_cursor.count():
                queued_cursor = col.queue.find({"status":"queued"}).limit(my_limit)
                if queued_cursor.count():
                    for doc in queued_cursor:
                        print col.queue.update({"_id":doc['_id']},{"$set":{"status":"processing"}} )
                # print col.queue.update({"status":"queued"}, {"$set":{"status":"processing"}}).limit(10)
                    processing_cursor = col.queue.find({"status":"processing"})
            if processing_cursor.count():
                time.sleep(1)
                for doc in processing_cursor:
                    ############################# THIS LINE HERE ######################
                    new_file_name = ona_sum_tool.run_summary(dt_ona_sum_format(doc['start']), dt_ona_sum_format(doc['end']))
                    # print "new_file_name: ", new_file_name
                    old_id = doc['_id']
                    # print old_id
                    doc['_id'] = str(new_file_name)
                    doc['status'] = 'done'

                    insert_result = col.queue.insert(doc)
                    if(insert_result):
                        col.queue.remove({"_id":old_id})

Error: 错误:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    from lib.threads import ConnectToDBThread, StartBottleThread, ProcessOutputController, \
  File "C:\dev\myProject\myProject\webapp\src\lib\threads.py", line 10, in <module>
    from myProject import ona_sum_tool
ImportError: No module named onager

Perhaps you have forgotten to put the the __init__.py file in your lib folder. 也许您忘记将__init__.py文件放在lib文件夹中。 Here's why you need the __init__.py file : 这就是你需要__init__.py文件的原因

The init .py files are required to make Python treat the directories as containing packages; 需要init .py文件才能使Python将目录视为包含包; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. 这样做是为了防止具有通用名称的目录(例如字符串)无意中隐藏稍后在模块搜索路径上发生的有效模块。 In the simplest case, init .py can just be an empty file, but it can also execute initialization code for the package or set the all variable, described later. 在最简单的情况下, init .py可以只是一个空文件,但它也可以执行包的初始化代码或设置all变量,稍后描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM