简体   繁体   English

如何从PyCharm导出一个Python程序

[英]How to export a Python program from PyCharm

I have a Python project created in PyCharm. Now that it's finished I want to make it available without PyCharm (making it an executable is not relevant).我在 PyCharm 中创建了一个 Python 项目。现在它已经完成了,我想让它在没有 PyCharm 的情况下可用(使其成为可执行文件是不相关的)。 It consists of different packages and quite a few files inside each package. How can I export the project so I can run it from one file that will call the rest?它由不同的包和每个 package 中的许多文件组成。如何导出项目以便我可以从一个将调用 rest 的文件中运行它?

If you do not want to publish the project and just use it for yourself:如果您不想发布项目而只想自己使用:

  1. Create a new __main__.py in your project root directory and start your program from there (by importing the old main.py etc.)在您的项目根目录中创建一个新的__main__.py并从那里启动您的程序(通过导入旧的main.py等)

  2. Make sure the program runs as expected: Run python __main__.py [arguments] in the root directory.确保程序按预期运行:在根目录中运行python __main__.py [arguments]

  3. If this works, zip the whole project directory using an archiver and use like this:如果可行,请使用存档器压缩整个项目目录并使用如下方式:

    python myproject.zip [arguments]

So the answer to your question is that it's complicated, but what you want to do is package your app and make it distributable.所以你的问题的答案是它很复杂,但你想要做的是 package 你的应用程序并使其可分发。 This means including the python interpreter in the app as well as all of its dependancies so that anyone can use it without first installing python or anything additional to your app.这意味着在应用程序中包含 python 解释器及其所有依赖项,以便任何人都可以使用它而无需先安装 python 或您的应用程序的任何其他内容。

BeeWare's Briefcase does the heavy lifting in packaging in this way: BeeWare 的公文包以这种方式完成了包装中的繁重工作:

Visit their github and follow the tutorial: https://github.com/beeware/briefcase访问他们的 github 并按照教程进行操作: https://github.com/beeware/briefcase

Think of PyCharm as a notepad/gedit - text editor of sorts.将 PyCharm 视为记事本/gedit - 各种文本编辑器。 You can simply save the file with any name and a .py extension.您可以简单地使用任何名称和.py扩展名保存文件。

Now, to make it interpretable by Python interpretor, do the following:现在,要使其可被 Python 解释器解释,请执行以下操作:

import math  # an example import
# other imports


class SomeClass:
    def __init__(self):
        self.x = None

def someFnc():
    pass

def starting_point():
    # Make sure your logic begins execution here
    # e.g. use someFnc() or SomeClass here

if __name__ == '__main__':
    starting_point()

Above template will make your .py file ready for interpretation.上面的模板将使您的.py文件准备好进行解释。 That is to say, on a shell/cmd you can write this:也就是说,在 shell/cmd 上你可以这样写:

$> python your_file.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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