How to Run Python Script from Another Machine Without Installing Imported Modules

How can I run a Python project on another computer without installing anything on it?

I see two options.

  1. Use an online IDE and Python Interpreter (assuming you did not have internet for downloading Python, but do have internet in general). I suggest replit.

  2. Use a portable version of Python. Those are available in the official website and are called "Windows embeddable package". You can test downloading it to a usb, and running it in some computer without Python; it should work.

Running python script without installed libraries

Python first "compiles" a program into bytecode, and then throws this bytecode through an interpreter.

So if your code is all Python code, you would be able to one-time generate the bytecode and then have the Python runtime use this. In fact I've seen projects such as this, where the developer has just looked through the bytecode spec, and implemented a bytecode parsing engine. It's very lightweight, so it's useful for e.g. "Python on a chip" etc.

Problem comes with external libraries not entirely written in Python, (e.g. numpy, scipy).

Python provides a C-API, allowing you to create (using C/C++ code) objects that appear to it as Python objects. This is useful for speeding things up, interacting with hardware, making use of C/C++ libs.

Run python script with module/ modules already imported

I think what you'r looking for is the interactive mode:

-i

When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command

So just use python -i cls.py in your batch file. This would import your Python file and stay in the Python interpreter prompt. You could then just call cls() because it's already imported.

Alternatively, you could set the environment variable PYTHONSTARTUP in your batch file:

If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.



Related Topics



Leave a reply



Submit