[英]py2app not including pythonpath
I'm trying to build an app with py2app. 我正在尝试使用py2app构建一个应用程序。 I can build the app but when I run it I get
ImportError
in the console, the import error is that there is No module named PythonApp
which is the folder all my source is in, the location of which is in my PYTHONPATH
. 我可以构建该应用程序,但运行该应用程序时,在控制台中出现
ImportError
,导入错误是No module named PythonApp
,这是我所有源代码所在的文件夹,其位置在我的PYTHONPATH
。
I have been importing local files like from PythonApp import file
instead of just import file
to help avoid namespace issues. 我一直在
from PythonApp import file
导入本地文件from PythonApp import file
而不仅仅是import file
以帮助避免名称空间问题。
I have tried to build it with the following flag python setup.py py2app --use-pythonpath
but this hasn't appeared to make any difference. 我尝试使用以下标志
python setup.py py2app --use-pythonpath
来构建它,但这似乎没有任何区别。
import file
? import file
? PYTHONPATH
? PYTHONPATH
? If I understood your problem well, you have the following layout: 如果我很了解您的问题,那么您将具有以下布局:
PythonApp/
__init__.py
file.py
foo.py
And you try to import the module file
from foo.py
by from PythonApp import file
. 并尝试导入模块
file
从foo.py
通过from PythonApp import file
。 If this the only reason you want to set PYTHONPATH
, there is a simpler solution: use relative imports : 如果这是您要设置
PYTHONPATH
的唯一原因,那么有一个更简单的解决方案:使用相对导入 :
from . import file
You can use from .. import file
in a sub-package of PythonApp
, and so on. 您可以在
PythonApp
的子包中使用from .. import file
,依此类推。 This way you can avoid name collisions with standard modules. 这样,您可以避免与标准模块发生名称冲突。
If you need to hack the import path for some other reasons, you can also set the sys.path
variable in the startup script (probably py2app has some options for that, too). 如果由于其他原因需要破解导入路径,还可以在启动脚本中设置
sys.path
变量(可能py2app也有一些选项)。 Keep in mind though that if you add external directories into the import path, it will be harder to distribute the app bundle. 请记住,如果将外部目录添加到导入路径中,则分发应用程序捆绑包将更加困难。
Also, a more trivial explanation for the ImportError
is that py2app did not copy your package into the app bundle. 另外,对于
ImportError
的更简单的解释是py2app没有将您的包复制到应用程序包中。 Make sure you have all your packages listed in the packages
parameter of setup()
. 确保在
setup()
的packages
参数中列出了所有软件包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.