我具有以下文件夹结构:
app
__init__.pyutils
__init__.pytransform.pyproducts
__init__.pyfish.py在fish.py中,我transform按以下方式导入:import utils.transform。
当我从Pycharm运行fish.py时,它运行得很好。但是,当我从终端运行fish.py时,出现了error ModuleNotFoundError: No module named 'utils'。
我在终端中使用的命令:来自app文件夹python products/fish.py。
我已经研究了这里建议的解决方案:从不同的文件夹导入文件,将应用程序文件夹的路径添加到sys.path帮助中。但是我想知道是否还有其他方法可以使它工作而无需向中添加两行代码fish.py。这是因为我在/ products目录中有很多脚本,并且不想在其中每个脚本中添加两行代码。
我研究了一些开源项目,并且看到了很多示例,这些示例是从并行文件夹中导入模块而不向sys.path中添加任何内容的,例如:https : //github.com/jakubroztocil/httpie/blob/master/httpie/plugins /builtin.py#L5
如何以相同的方式使其适用于我的项目?
我在Rails 3中使用acts_as_taggable_on v.2.0.3来为帖子添加标签.我添加了一个标签云,如下所述:https://github.com/mbleigh/acts-as-taggable-on,但我遇到了一个错误: Posts中的ActionController :: RoutingError #index:没有路由匹配{:action =>"tag",:id =>"politics",:controller =>"posts"}.我的代码如下:
PostHelper:
module PostsHelper
include TagsHelper
end
Run Code Online (Sandbox Code Playgroud)
模特岗位:
class Post < ActiveRecord::Base
...
acts_as_taggable_on :tags
end
Run Code Online (Sandbox Code Playgroud)
PostController中
class PostController < ApplicationController
...
def tag_cloud
@tags = Post.tag_counts_on(:tags)
end
end
Run Code Online (Sandbox Code Playgroud)
视图:
<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
routes.rb中:
Blog::Application.routes.draw do
root :to => "posts#index"
resources …Run Code Online (Sandbox Code Playgroud)