简体   繁体   English

py2app不包括pythonpath

[英]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来构建它,但这似乎没有任何区别。

  • Should I just change the import statements throughout to import file ? 我是否应该整个更改导入语句以import file
  • How can I make py2app realise my PYTHONPATH ? 如何使py2app实现我的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 . 并尝试导入模块filefoo.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM