How to copy a Python module and it's dependencies to a file

回眸只為那壹抹淺笑 提交于 2019-12-24 18:27:24

问题


I want to use a Python module like urllib.request but have all the module's dependencies in a file where I can use them on a computer without having the entire Python installation.

Is there a module or tool I can use to just copy a specific module into a file and it's dependencies without having to go through the entire script and copying it manually. I'm using Python 3.


回答1:


There's a variety of options available to do this sort of thing. The one that comes to mind first off the top of my head is python's built-in zip support; if you have a __main__.py in your zip, running python on the zip file will run it as a script. Simple (pure-python, non-OS-dependent) packages should be simply embeddable into such a zip file.

Alternatively, tools such as cx_Freeze can package a script with its dependencies including the interpreter in an OS-specific fashion. This will typically result in a larger footprint as the resulting bundle includes the interpreter as well, but allows including more complex dependencies with native components; I'm not sure this is possible with python's zip import functionality.




回答2:


Use a container tech. Docker, for example, gives you the ability to port your code with dependencies to any machine you want without you have to install anything new in the machine and it saves a lot of time too.



来源:https://stackoverflow.com/questions/46383369/how-to-copy-a-python-module-and-its-dependencies-to-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!