ValueError:在非包中尝试相对导入

时间:2015-09-11 17:51:35

标签: python python-2.7

我有两个Python文件,一个存储在// store the app in a variable so you can write more modules / directives with it var app = angular.module('app', ['ngRoute', 'ui.bootstrap' ]) // Retrieves Posts app.controller('Main', function($scope, $http, $routeParams, $q){ // create a deferred object var def = $q.defer(); var promises = []; // Array of promises for our deferred wrapper // All HTTP requests return a promise object; push it on to our array promises.push($http.get('wp-json/posts/').success(function(res){ $scope.posts = res; })); // Push the 2nd promise promises.push($http.get('wp-json/media/').success(function(res){ $scope.media = res; })); // Process when all are complete def.all(promises).then(function(res) { // Successful (promise resolved) // Loop through the results in $scope.posts / $scope.media, build your objects }, function(res) { // Failed (promise rejected) // Do something here if a promise is rejected }); }); 位置,另一个存放在位置/Python/plata.py中。这就是我的/Python/tao/mock.py文件::

plata.py

我正在尝试在def printSomething(): print 'This is a test.' 文件中导入printSomething()函数,如下所示:

mock.py

然而,这是我遇到的错误:

from . import plata

plata.printSomething()

我已将Traceback (most recent call last): File "/home/manas/Python/tao/mock.py", line 1, in <module> from . import plata ValueError: Attempted relative import in non-package 个文件也包含在地理位置__init__.py/Python/__init__.py中。但是,我仍然遇到同样的错误。

这里看起来有什么问题?

2 个答案:

答案 0 :(得分:4)

出于明显的安全原因,包的父目录 not 包含在sys.path中。但是,无论如何......

import sys
sys.path.append('..')

import plata

希望这能帮到你!

答案 1 :(得分:2)

有关Module vs Package的说明,请参阅What's the difference between a Python module and a Python package?。缺点是您的Python目录不是包。 plata.py是一个独立的模块,应该导入import plata

相关问题