How to Install a Script to Run Anywhere from the Command Line

How do I install a script to run anywhere from the command line?

The best place to put things like this is /usr/local/bin.

This is the normal place to put custom installed binaries, and should be early in your PATH.

Simply copy the script there (probably using sudo), and it should work for any user.

Run Python-Script anywhere on Windows

try to create a "code.bat" file with this command "python script.py" inside your .bat file then add the "code.bat" to your path in your environments anytime you want to run the script just type code in your shell.

Windows - how can I run a Python file in any cmd directory?

well first you have to turn it into an executable
by using pyinstaller

pip install pyinstaller

open the main directory

pyinstaller --onefile your-script.py

then copy the executable from the dist folder into a folder added into the PATH

example:

C:\Windows

or add the custom folder into the PATH
from Control Panel\System and Security\System
and then Advanced system settings
, Environment Variables
and then system variables
and then edit the PATH with your path

and then you can execute it from any directory

How do I install and run a python script

In Python, you don't have to install arbitrary scripts (such as your referenced git repo).

Couple of general pointers:

  • Python is an interpreter based language, installed on your machine. It's literally an executable. A specific piece of python code (e.g. a .py file) can be run by feeding it into your python installation. I am guessing you are using a Mac, so running "python3 file.py" in your command line would execute python, which would then execute file.py.
  • Python is very extensible. It consist out of many packages that provide functionality. This functionality can then be imported into your own code (with import package_name). Python comes with many in-built packages (for example, math, typing, re).
  • You can add packages to your Python installation by using pip. Pip is a command line tool to manage (e.g. find, install, uninstall, update) python packages. Well known packages that people tend to install are numpy, pandas, requests.

Knowing all that, using Python comes with a challenge. Executing python code is depending on the installation of Python itself of the machine it is running on. For example, on my Python installation, I have a package installed that is used in a python script. The script works perfectly well on my machine. However, if you were to run this script (and didn't have that specific package installed that the script is relying on), it would fail miserably with a ModuleNotFound error. So, we needed a way to communicate what dependencies a script has.

This is where requirements.txt came in. It describes what packages must be installed (and which version!) in your Python installation. You can then use pip to install said packages, after which you should be able to run the scripts.

Your situation

You found some python script that you want to run. Note that this is not a package that need to be installed, you just want to run main.py. However, this code is 4 years old (which is a long time, in terms of IT years). Specifically, from the top of my head, Python 3.5 was current back then (now we are at 3.10, you are running 3.9 which is completely fine by the way). That is important, as not everything is backward compatible.

The reason why I bring this is up is the following; you are trying (correctly!) to install requirements.txt, to ensure the right packages are installed in your Python installation that are used by this script. However, the versions of these packages are quite old and are not guaranteed to work on newer Python installations. And that is exactly the case here.

You already had the package installed in your Python installation (see the line Successfully uninstalled numpy-1.22.4), which pip uninstalled for you in order to try and install an older version (from requirements.txt: 1.14.3). That version is not compatible with your python installation.

Luckily for you, you don't need the old versions. I tested it for you, and you can run the script with the current versions of the dependencies.

Second, you try to install the script as a package (with pip3 install .). This tells pip (or pip3, in your case) to look for an installable package in the current work folder and add it to your python installation. That wouldn't work, as this script is not a package. I hope this is clear now :)

How to run your script? Try the following commands:

  1. pip3 install numpy
  2. pip3 install matplotlib
  3. python3 main.py -> this must be executed from your working folder (e.g. where main.py lives, otherwise you can do python3 /full/path/to/main.py

One more general note

Your python installation can become 'dirty' with all kind of packages installed. That leads to situations that some script don't work properly, although you have installed all dependencies correctly. For example, another package that you have installed can interfere with one of the dependencies, causing unintended side effects.

To mitigate this problem, Python has the concept of 'virtual environments'. This basically makes a clean copy of your Python installation (without user installed packages) for you to work in. This is especially handy when working with multiple users on the same project and you want to make sure everybody is running the same Python installation.

This would help you in the future as well, so I am sharing it just in case (although it is a bit off topic from your original question).

To create a virtual environment, we can use the following commands;
python3 -m venv venv
This creates a folder venv in your current folder, with its own Python installation (including it's own pip!). However, when using python3 main.py, the python that is invoked is still your 'global' python installation. To start using your new 'virtual' python installation, you need to activate it:
source venv\bin\activate
You will see that your command line will be prepended with (venv) - and
now you will use a clean python installation when running python file.py. If you use pip install numpy now, it will install it into the virtual environment, not in your global python3 installation.

EDIT: one more thing that is MacOS specific; you have python and python3 available to you (globally), the first will refer to python2 and should not be used. However, if you make a virtual environment, you can use either and both are pointing to the virtual python installation.

How to use a wrapper script for a Raku CLI

So I'm guessing Zef adds it?

CompUnit::Repository::Installation adds it

Why is it there? One reason is because some wrapper needs to manage bin scripts the same name that would otherwise clash if they were in the same directory.

Is there some way to use the run-script method shown above without Zef?

run-script is for CompUnit::Repository::Installation only -- if you aren't installing a module then run-script won't be of interest

Assuming I do need to have my users download two files, where should I have them install the files?

Well the recommended/idiomatic way would be to use the core raku functionality to install things (i.e. use zef). Otherwise where you should put some code is either a) not going to matter or b) going to be mostly dependendant on your environment, what is idiomatic for your os, etc.

Does it need to be copied into their Raku module search path (and, if so, what command would show them what that path is)

echo $RAKULIB should suffice for showing the module search path in most cases, especially if they aren't interested in where the installation paths are. And as such you can instruct users to set e.g. RAKULIB=$FROB_LIB_DIR to point wherever your library is if you want them to be able to run your script without manually specifying it via raku -I../ frobnicate (so they don't copy the code anywhere special, they just point to wherever they e.g. clone your repo). Ditto for working with $PATH.

Or can I modify the frobnicate wrapper in some way (maybe by changing raku to raku -Ilib or something like that?) in a way that would let them install both to directory on their PATH?

I would advise against installing things based on some value in $PATH. Instruct users to set $PATH, don't install things to $PATH.

Technically you could add use lib '../' to your script, but using use lib in a script that you also want users to install normally is less than ideal since its adding an unused, potentially hijackable module search path when being run from such an install.

If you want your code to precompile then I suggest putting it in a module, and instructing your users that, if they don't intent to install it, to invoke it via raku -I../ ./frobnicate on a per-use basis or something like export RAKULIB="$FROB_LIB_DIR,$RAKULIB" followed by ./frobnicate for something more permanent. Alternatively if someone evenetually implements precompilation of scripts then you could just use the single file approach.

python scripts with shebang for Windows

Not sure if I understood your question correctly, but I guess you want to run the command 'nfile' from anywhere in Windows command line.

This is possible with the help of another batch (.bat) file. You need create a file called 'nfile.bat' next to the script.
Both the script and batch file need to be in a location that's defined in PATH.

The content of the batch file should contain something like:

python nfile.py

Running a python installed script within a virtual environment looks for the global Python installation

After reinstalling and modifying Python38 multiple times (at C:\Python38 directly this time), and trying different solutions, I finally was able to fix the issue somehow.

Here's a few related issues that might help others with the same issue:

  • https://bugs.python.org/issue40253
  • https://stackoverflow.com/a/67894642/13123426
  • How to associate Python scripts with active virtualenv?

The last thing I did was:

  1. Setting again assoc .py=Python.File which corresponds to the Python Launcher (it was already like this before, but I had changed it in between with the solution proposed at https://stackoverflow.com/a/67894642/13123426 which did not work for me).

At this point, this was still not working, the cmd kept asking me which program I wanted to use, then I got a flashing window (script opening then window closing at the end).


  1. Right-click on any python script, then choose "Open with" and select "Python Launcher"

  2. Open cmd again, activate my virtual env, python myscript.py works finally !

The weird thing is that I always checked "associate .py files" in the Python installer, as well as "install pylauncher"...



Related Topics



Leave a reply



Submit