我有一个用户定义的模块(称为test.py),
def getLastDate(currDt):
prevDt = (currDt - dt.timedelta(days=1))
return prevDt
我正试图从另一个python脚本中调用它:
import datetime as dt
import test
currDt = dt.date.today()
print(test.getLastDate(currDt))
问题是,当我运行它时,它的范围是如此之大,以至于内部模块(test.py)无法找到dt模块并给出以下错误:
1 def getLastDate(currDt):
----> 2 prevDt = (currDt - dt.timedelta(days=1))
3 return prevDt
NameError: name 'dt' is not defined
我是否必须在test.py模块中重新加载datetime模块?在我看来这不合适。
预先感谢您的帮助。
答案 0 :(得分:0)
您还需要在test.py文件中导入dt
package es.test.config;
import java.util.Map;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;
import org.eclipse.jdt.annotation.Nullable;
import org.primefaces.application.exceptionhandler.PrimeExceptionHandler;
import org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory;
/**
* Extended primefaces exception handler factory in order to create a exception
* handler that redirects to the desired error page.
*/
public class ExtendedPrimeExceptionHandlerFactory extends PrimeExceptionHandlerFactory {
private static final String ERROR_PAGE = "error.xhtml";
public ExtendedPrimeExceptionHandlerFactory(final ExceptionHandlerFactory wrapped) {
super(wrapped);
}
@Override
public ExceptionHandler getExceptionHandler() {
return new ExtendedPrimeExceptionHandler(getWrapped().getExceptionHandler());
}
private static class ExtendedPrimeExceptionHandler extends PrimeExceptionHandler {
public ExtendedPrimeExceptionHandler(ExceptionHandler wrapped) {
super(wrapped);
}
@Override
protected String evaluateErrorPage(@SuppressWarnings("null") Map<String, String> errorPages,
@Nullable Throwable rootCause) {
return ERROR_PAGE;
}
}
}
答案 1 :(得分:0)
是的,按照编写的方式,您需要在test.py中导入日期时间。
如果要避免两次导入日期时间,则可以将其导入到test.py中,然后像下面这样重写函数:
def getLastDate(currDt, delta):
prevDt = (currDt - dt.timedelta(days=delta))
return prevDt