2

I made a python (3) package and I have been trying to upload it on Github. I also know how to push and install a git using pip. To test if it works as anticipated, I made a virtual environment on my local computer (linux) and pip installed my already pushed private package in there without a problem.

The issue is that I don't know how to access it!!! (I know how to activate and use virtualenvs; I don't know how to call my package) My package has a main interface that one would need to call it in terminal as follows:

python3 myui.py some_args *.data

and it's supposed to create some files where it's called. In other words, it's not exactly a module like numpy to be imported. I have watched/read many tutorials and documentations on the web and tbh I'm lost here.

cutus_low
  • 85
  • 5
  • 1
    Pip installs packages in the /lib/pythonX.Y/site-packages subdirectory of the virtualenv folder (https://stackoverflow.com/questions/29980798/where-does-pip-install-its-packages). Is your package code in the that location? If it is, site-packages should be in your PYTHONPATH by default so you can call import your_package_name – Dan Ruswick Apr 25 '21 at 03:09

1 Answers1

1

You are looking for the -m flag. If you installed everything correctly, then the following command should allow you to run your script (based on your example). Note that you shouldn't add the file extension '.py'.

python3 -m myui some args *.data


If you have an actual package (directory with __init__.py file and more) instead of a module (a single .py file), then you can add a __main__.py file to that package. Python will execute this script when you use the -m flag with the package's name, in the same way as shown above.

python3 -m mypackage some args *.data

If you want to run a different script that is nested somewhere inside of that package, you can still run it by specifying its module name:

python3 -m mypackage.subpackage.myscript some args *.data


Another common way to make your script available uses the setup script (setup.py) or setup configuration file (setup.cfg) that is used to install the module or package. In that case, you can add an entry point to map a command to a specific module/function/etc. (as described in this Python packaging tutorial) so that you can run that command instead of having to use the -m flag with Python.

$ mycommand some args *.data