简体   繁体   中英

How can I include a pip installed module with my python script?

I wrote an encryption program in Python 3.6 that uses the module pycryptodome , specificly these imports:

from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto.Random import get_random_bytes

The program works, but it cannot be used without the user installing the pycryptodome module themselves.

Is there any way to include that package somehow or can I create a "first time setup" to install it for the end user?

Short answer - as already mentioned in comments - you need to package your script.


Long answer - I have been there before for the first time - it's annoying. They way Python modules and scripts are packaged is constantly evolving and not all available documentation is being kept up to date. You can easily end up reading recent documentation which, by the time of reading, is already flawed (ie obsolete).

The easiest approach, without going down all the way, is a simple file for pip , which describes your project's dependencies. It's called requirements.txt . Up-to-date information can be found here .

If this is not enough, you have to package your application. A good but little maintained and somewhat outdated overview / introduction can be found here . It's good for a start, but do NOT try to follow it to the letter! Another OUTDATED manual for beginners commonly cited can be found here . Read it for basic understanding and do NOT try to follow it to the letter.

Once you have come to this point, it's time to read "What the Hell? -- A Journey Through the Nine Circles of Python Packing" . It gives a lot of practical advise. The most practical of all: Look at how other projects are doing it and copy-paste it ... ehm ... learn from it.

If you are not scared off by now, I can actually recommend to look at Python module templates. One of the best ones I know of is the "Cookiecutter PyPackage" . Its well maintained documentation is available here . If you learned the basics of Python packaging, it's a quick and reliable way of creating all the files and infrastructure required for packaging your code.

Honorable mention: There are tools, which try to streamline the entire process. Number one on my list and also already mentioned in the comments is PyInstaller ( manual ). Another tool (for Windows), which is usually mentioned, is py2exe (no up-to-date documentation available, AFAIK). Another up-and-coming and promising (yet not production-ready?) tool is Briefcase ( documentation ). There are more of those, but they all have their issues. There is a good chance you end up reading into the above literature trying to understand those issues ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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