Python:“__ init__”不会加载模块

时间:2017-05-03 11:14:44

标签: python

以下教程我试图创建自己的包但没有成功。包含该包的文件夹的路径位于PYTHONPATH,包文件夹中包含“ init .py”文件及其模块。

mypath/my_packages/mymath
---------------------/__init__.py 
---------------------/add.py 
---------------------/division.py
---------------------/(other .py files)

/ my_packages的路径在PYTHONPATH中。我也可以在“sys.path”中看到它。

如果__init__.py为空,我可以“导入mymath”,但不能使用任何模块。 如果我尝试从__init__.py导入模块(from add import add [模块add.py包含一个函数“add()”]),我收到一个错误: “ImportError:没有名为'add'的模块。”

1 个答案:

答案 0 :(得分:2)

如果org.apache.jasper.JasperException: An exception occurred processing JSP page /create_user.jsp at line 21 18: String username=request.getParameter("username"); 19: String password=request.getParameter("password"); 20: String email=request.getParameter("e-mail"); 21: if(username.length()<2 || password.length()<6 || email.length() <5){ 22: out.print("Username must be atleast 2 characters long, Password must be atleast 6 characters long, E-mail must be atleast 5 characters long"); 23: return; 24: }else{ Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWra pper.java:568) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:4 70) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause java.lang.NullPointerException org.apache.jsp.create_005fuser_jsp._jspService(create_005fuser_jsp.java:81) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:4 32) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:727) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在PYTHONPATH中,您应该导入类似

的包
path/to/my_packages

或者,如果您在mymath模块中(例如在from mymath.add import add 中),您也可以进行相对导入,例如

__init__