Search code examples
pythonpippackage

Packaging python script with it's dependent packages in single packages


I have a situation:

In macOS machine with Python 3.6.8, I wrote single python script called publisher.py which uses pycrypto package. Everything runs as expected.

Now, I need to put my code in a shared location of production Linux env, from where hundreds of developers will run the script with just Python 3.6.8 in their machine. I don't have rights/privilege to install pycrypto in production Linux env, I can just copy something there.

Obviously, I can't force all developers to do pip install pycrpto on their individual env.

Is there a way, I can package my single script with it's dependency(pycrypto) and copy it to production Linux env?

Please suggest.


Solution

  • Answering myself, to help others as I found solution: In short, question is "How I can make a package downloaded using pip locally available in a project, so that I can send this project to some other machine and they don't need to install any package from internet. Rather the project dependencies are resolved locally within the package."

    Solution is: Step.1) Copy the package directory Crypto from standard library package like: PyCharm_Workspace/pythonProject/.venv/lib/python3.6/site-packages or /usr/lib/python2.7/site-packages alongside your python script. In my case, I have a single python script with a dependency package pycrypto

    Step.2) In your python script set the system environment variable to the path of locally copied package directory. Because, the script and package directory copied are in same location. Hence, this two lines would do the trick.

    import sys
    import os
    print(os.getcwd())
    file_path = os.getcwd() #'/Users/OSAO/Documents/PyCharm_Workspace/'
    sys.path.append(os.path.dirname(file_path))
    
    # Now below dependencies are resolved.
    from Crypto.Hash import SHA256
    from Crypto.Signature import PKCS1_v1_5
    from Crypto.PublicKey import RSA
    

    Helpful link: https://www.tutorialspoint.com/How-we-can-import-Python-modules-without-installing