-3

I have written a script (around 2k lines) for processing text.

It reads the input form my text file, and print the output in another file.

But, I want it can be run on any other laptop (with Python installed) easily as well. For example, other people can run it without installing additional libraries (that I had imported in the script).

How can I realize my purpose? By packaging my script in a library or what else I can do? Please provide any hint.

I tried to use the pyinstaller or the py2exe, but I always have a problem of over recursion limit, and since I have several huge sized libraries being imported, so I guess even I can finally make a .exe file, it would be in a huge size, so I stopped to using that way. Anyone has a comment on it?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Melina
  • 293
  • 3
  • 11
  • 1
    The recursion problem sounds like a bug in your code. If your code has library dependencies, there is no way to run it without those dependencies installed. If not, just don't include those libraries at all. – tripleee Dec 22 '21 at 13:37
  • I guess maybe not, because I can run my file correctly, but when I use the pyinstaller or the py2exe, in the terminal, it reminds the recursion problem. – Melina Dec 22 '21 at 13:38
  • Does this answer your question? [Export Python script](https://stackoverflow.com/questions/15069839/export-python-script) – Tomerikoo Dec 22 '21 at 13:39
  • Just write a setup.py script for your project, and run a `pip install -e .` - I don’t think it can get easier than that tbh – rv.kvetch Dec 22 '21 at 13:39
  • A last resort would be to deploy your code on a service like heroku and distribute a script to interact with the server. – Sujal Singh Dec 22 '21 at 13:51

1 Answers1

1

If you're sure that every client has Python and pip installed and present in PATH, you can just pip install the libraries in the beginning of your script. Something like this:

import subprocess
subprocess.run(['pip', 'install', '--user', 'your', 'libs'])
import your
import libs

This is just a general idea, maybe hacky, and definitely requires additional work with error handling, etc.

Expurple
  • 877
  • 6
  • 13
  • thanks, I am trying to use this way – Melina Dec 23 '21 at 15:02
  • could you please explain more to me about the parameters, for example, what is 'your'? does it mean my code? but if so, I import my code in my code? Thanks! I am very new to this. – Melina Dec 23 '21 at 15:45
  • `your` and `libs` are just placeholders, replace them with the libraries that you're using. I should've made it clearer in my answer – Expurple Dec 23 '21 at 17:12
  • Thanks @Expurple, if I understand you correctly, I put all the required libraries for my code into the subprocess.run(['pip', 'install', '--user', 'lib1', 'lib2',, 'lib...' 'libn']), then, these libraries will be installed automatically (when a laptop has not installed these libs) before start run the code. Am I correct? Thanks again! ps: I tried on my laptop, it looks like that, but I am going to try it on another laptop again. – Melina Dec 24 '21 at 11:05
  • Yep, that's the idea. I would also check how your script behaves if subprocess fails. For example, if pip is not installed on the system, or there's no internet and it can't download the libraries. Of course, your script is gonna fail without the libraries anyway, but maybe you need to make the error message nicer or print your contacts for reporting the problem, idk. – Expurple Dec 24 '21 at 11:15
  • Thanks, it works now. I use subprocess.run for each required external library. For example: subprocess.run(['pip', 'install', '-U', 'python', 'lib1']), subprocess.run(['python', 'install', 'lib2']). Inside of the '[ ]', I put the complete install command with just separate each word(text) into a single quota ' ' and conjunct them with comma (,), then it works very well. Thanks again! – Melina Dec 24 '21 at 13:23