0

I would like to be able to run a Python script from any folder. However, this Python script, named statistics.py calls upon two other files, colorbar.py and language_bytes.py. It also reads data from a folder that is located beneath it, in data/languages.yml.

Because it has these dependencies, along with the .yml file, I have been unable to have it be executable from anywhere. Even if I add #!/usr/bin/env python3 to the first line of statistics.py and drag it, along with the other two files and the data folder into usr/local/bin, this doesn't work.

With a little bit of digging I found out that this was because the data folder was being deleted as soon as it went into usr/local/bin. This means that the Python script is unable to access the data that it needs, and so cannot be run.

Is there any other way to run a Python script globally?

Edit: Because this script is made to by modifiable by users and will be open-source, it would be optimal if it could be run without having to be bundled into a PyPI package. As well as this, it doesn't make sense that every time one would want to make a globally runnable script with resources they'd have to upload it to PyPI, even if it was just for personal use.

Destaq
  • 655
  • 9
  • 23
  • You just have to define the absolute path of the resources :-) – mama Jul 08 '20 at 19:14
  • @mama I would like to distribute this program. Is there any place I can store these resources that would work on any platform/computer/configuration? – Destaq Jul 08 '20 at 19:19
  • @Destaq, then you should [package it](https://packaging.python.org/). This topic is too broad to cover generally on Stack Overflow. – ChrisGPT was on strike Jul 08 '20 at 19:20
  • Are you looking to convert it into a .exe? This way, it will store all its data inside the file itself and could be runnable without a python installation on any windows (linux too?) computer. You could try using auto-py-to-exe. – Bhavye Mathur Jul 08 '20 at 19:23
  • @BhavyeMathur I would like it to be cross-platform, and I myself am a Mac user. Creating a .exe file is not what I am looking to do. – Destaq Jul 08 '20 at 19:26
  • take a look at [python packaging](https://packaging.python.org/tutorials/packaging-projects/) maybe this is what you are looking for, I also had a project in which I need some json files I just packaged it and then it worked like a charm. also you can add `entry point` then you can run your script with custom command. – Sarthak Kumar Jul 08 '20 at 19:29
  • @Destaq Hmm.. to my knowledge, there is now way to do this with only 1 file. Even if you look at professional software, they have different installs for Macs & Windows. As the comments here say, you could try python packaging but that requires the user to have python installed. If not, you can look into creating .app files for Macs and .exes for windows. – Bhavye Mathur Jul 08 '20 at 19:31
  • I would vastly prefer being able to have this runnable without it being a package. A package cannot be modified by the users (I would like users to be able to customize/modify parts of the code to suit their needs) and it would be an inferior option to having this runnable normally anywhere. – Destaq Jul 08 '20 at 19:32
  • @Destaq I think users can edit a package from where it is installed, I've done that, also they can just clone your source from github and build it after editing. – Sarthak Kumar Jul 08 '20 at 19:34
  • @BhavyeMathur I am expecting them to have Python installed (therefore don't want to make .apps or .exes) but I would want it to be runnable anywhere. Is there at least some accessible location that is universal for all users where I could store this data? – Destaq Jul 08 '20 at 19:34
  • @Destaq, are you talking universal in the sense that anywhere around the world could access the same data? or that they could access it from anywhere within their computer? – Bhavye Mathur Jul 08 '20 at 19:37
  • @BhavyeMathur Universal as in works for linux + windows + macos – Destaq Jul 08 '20 at 19:39
  • Packaging doesn't preclude modification. I often [install packages as editable](https://stackoverflow.com/a/35064498/354577) as I'm working on them. I still think that's the right solution here. – ChrisGPT was on strike Jul 08 '20 at 19:41
  • @Chris well even then for future reference, if I wanted to make scripts like these multiple times for myself. There's got to be a better way then creating a PyPI package each time I want to have a globally available script just for myself. I can see other times others and I may want to make python scripts with resources, and using PyPI feels like the wrong route if it's just, say, personal. – Destaq Jul 08 '20 at 19:43
  • You don't have to actually publish it to PyPI. But you'll want to at least create a basic `setup.py` so you can do `pip install -e .` or similar. You're the one who brought PyPI up. I haven't mentioned it until now. (And, again, this is _too broad for SO_. Spend some time with the packaging docs, read other people's specific packaging questions, try it out, and come back if you have a specific, concrete question that isn't covered elsewhere.) – ChrisGPT was on strike Jul 08 '20 at 19:45
  • I'm just discussing it because I've had a few people state in the comments that they suggested packaging on PyPI. – Destaq Jul 08 '20 at 19:46
  • Searching for "PyPI" on this page I don't see anybody mention it before you do. Packaging != PyPI. – ChrisGPT was on strike Jul 08 '20 at 19:47
  • Well then I must have misunderstood. My knowledge of packaging only extends to pip and when I took a look at the link that @Sarthak dropped it extended to uploading a package to testpypi and eventually pypi. – Destaq Jul 08 '20 at 19:49
  • 1
    @Destaq it's upto you if you want to publish your package, other than that you can simply do `pip install -e .` in your project root after creating your setup.py to install that package locally which will then be accessed from anywhere in your system. also have a look at [example](https://github.com/BillMills/python-package-example) at github from here you can choose minimal configurations for you package – Sarthak Kumar Jul 08 '20 at 19:54
  • I have created the package and run `python3 -m pip install -e`. However, I am still unable to call it from the command line, it can only be imported. @Sarthak – Destaq Jul 09 '20 at 09:39
  • @Destaq did you specify entry point in your setup,py – Sarthak Kumar Jul 09 '20 at 10:13
  • @Sarthak No, I guess I will have to look into it. It's pretty hard for me to understand how to get this set up, I've been trying for a while now. I would also like to have it callable as `statistics` not `language_statistics.statistics`. – Destaq Jul 09 '20 at 10:35
  • I am still unsure of how to do this. Dropping it into `usr/local/bin` will only work for Linux and Darwin. Currently, I'm using `platform.system()` to create individual file paths... – Destaq Jul 09 '20 at 11:09
  • You can see my [setup.py](https://github.com/sarthakchaudhary13/sapera/blob/master/setup.py) in this I have used entry points. – Sarthak Kumar Jul 09 '20 at 22:11

0 Answers0