Robust way to ensure other people can run my python program

前端 未结 8 1346
醉酒成梦
醉酒成梦 2021-02-13 00:26

I wish to place a python program on GitHub and have other people download and run it on their computers with assorted operating systems. I am relatively new to python but have u

相关标签:
8条回答
  • 2021-02-13 00:38

    I think you can use docker with your python https://github.com/celery/celery/tree/master/docker

    kindly follow the files and I think you can figure out the way to make your docker file for your python scripts!

    0 讨论(0)
  • 2021-02-13 00:47

    If your program is less about GUI, or has a web GUI, then you can share the code using Google Colaboratory.

    https://colab.research.google.com/

    Everyone can run it with the same environment. No need for installation.

    0 讨论(0)
  • 2021-02-13 00:53

    Have you considered setting up a setup.py file? It's a handy way of bundling all of your... well setup into a single location. So all your user has to do is A) clone your repo and B) run pip install . to run the setup.py

    There's a great stack discussion about this.

    As well as a handle example written by the requests guy.

    This should cover most use cases. Now if you want to make it truly distributable then you'll want to look into setting it up in PyPi, the official distribution hub.

    Beyond that if you're asking how to make a program "OS independent" there isn't a one size fits all. It depends on what you are doing with your code. Requires researching how your particular code interacts with those OS's etc.

    0 讨论(0)
  • 2021-02-13 00:53

    Because it is missing from the other answers, I would like to add one completely different aspect:

    Unit testing. Or testing in general.

    Usually, it is good to have one known good configuration. Depending on what the dependencies of the program are, you might have to test different combinations of packages. You can do that in an automated fashion with e.g. tox or as part of a CI/CD pipeline.

    There is no general rule of what combination of packages should be tested, but usually python2/3 compatability is a major issue. If you have strong dependencies on packages with major version differences, you might want to consider testing against these different versions.

    0 讨论(0)
  • 2021-02-13 00:55

    In case converting all your python scripts into one executable can help you, then my answer below would help ...

    I have been developing a large desktop application purely in python since 3 years. It is a GUI-based tool built on top of pyqt library (python-bindings of QT C++ framework).

    I am currently using "py2exe" packaging library : is a distutils extension which allows to build standalone Windows executable programs (32-bit and 64-bit) from Python scripts; all you have to do is to:

    1. install py2exe: 'pip install py2exe'

    2. Create a setup.py script: It is used to specify the content of the final EXE (name, icon, author, data files, shared libraries, etc ..)

    3. Execute: python setup.py py2exe

    I am also using "Inno Setup" software to create installer: Creating shortcuts, setting environment variables, icons, etc ...

    0 讨论(0)
  • 2021-02-13 00:58

    There are many, many, many, many, many, many, many ways to do this. I'll skate over the principles behind each, and it's use case.

    1. A python environment

    There are many ways to do this. pipenv, conda, requirments.txt, etc etc.

    With some of these, you can specify python versions. With others, just specify a range of python versions you know it works with - for example, if you're using python 3.7, it's unlikely not to support 3.6; there's only one or two minor changes. 3.8 should work as well.

    Another similar method is setup.py. These are generally used to distribute libraries - like PyInstaller (another solution I'll mention below), or numpy, or wxPython, or PyQt5 etc - for import/command line use. The python packaging guide is quite useful, and there are loads of tutorials out there. (google python setup.py tutorial) You can also specify requirements in these files.

    2. A container

    Docker is the big one. If you haven't heard of it, I'll be surprised. A quick google of a summary comes up with this, which I'll quote part of:

    So why does everyone love containers and Docker? James Bottomley, formerly Parallels' CTO of server virtualization and a leading Linux kernel developer, explained VM hypervisors, such as Hyper-V, KVM, and Xen, all are "based on emulating virtual hardware. That means they're fat in terms of system requirements."

    Containers, however, use shared operating systems. This means they are much more efficient than hypervisors in system resource terms. Instead of virtualizing hardware, containers rest on top of a single Linux instance. This means you can "leave behind the useless 99.9 percent VM junk, leaving you with a small, neat capsule containing your application,"

    That should summarise it for you. (Note you don't need a specific OS for containers.)

    3. An executable file

    There are 2 main tools that do this at the time of writing. PyInstaller, and cx_Freeze. Both are actively developed. Both are open source.

    You take your script, and the tool compiles it to bytecode, finds the imports, copies those, and creates a portable python environment that runs your script on the target system without the end user needing python.

    Personally, I prefer PyInstaller - I'm one of the developers. PyInstaller provides all of its functionality through a command line script, and supports most libraries that you can think of - and is extendable to support more. cx_Freeze requires a setup script.

    Both tools support windows, Linux, macOS, and more. PyInstaller can create single file exes, or a one folder bundle, whereas cx_Freeze only supports one folder bundles. PyInstaller 3.6 supports python 2.7, and 3.5-3.7 - but 4.0 won't support python 2. cx_Freeze has dropped python 2 support as of the last major release (6.0 I think).

    Anyway, enough about the tools features; you can look into those yourself. (See https://pyinstaller.org and https://cx-freeze.readthedocs.io for more info)

    When using this distribution method, you usually provide source code on the GitHub repo, a couple of exes (one for each platform) ready for download, and instructions on how to build the code into an executable file.

    0 讨论(0)
提交回复
热议问题