Python Forum

Full Version: PyPI package and files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I run my program from the command line like this:
python3 myprogram.py {filename}
where {filename} is the datafile that myprogram processes.

Now I've created and installed it as a package I've tried:
python3
]>>>import mypackage

and
>>>from myapps import mypackage
both run, although obviously since I haven't mentioned the datafile, it won't be found.

I then tried referring to it like this:
>>>import mypackage {filename}
and
>>>from myapps import mypackage {filename}

but the syntax is invalid. Should I have stated that there would be a parameter in one of the files for PIP? Where will the program look for the datafile - the user's current directory?
Some relevant PyPi documentation or forum would be good, but I haven't been able to find either.

[edit: Changed the 'P' in Python in commands to lowercase]
mypackage was created by you? If so then it should be in the same folder as myprogram.py
(Oct-22-2021, 07:58 PM)ChrisOfBristol Wrote: [ -> ]Where can I find the documentation about running packages with command-line parameters?
Look at Entry Points
This is the way to make real command line programs in Python,so your Python3 myprogram.py {filename}
will become myprogram {filename}.
setup.py entry_point will make a executable and place it in Python root Scripts folder,
will even for Windows create an .exe file.
Then all that do install pip install your_module will have myprogram access at there command line.

Can look at this Thread where i use Click(my clear favorite when making CLI) and setup.py entry_point.
(Oct-22-2021, 08:49 PM)Axel_Erfurt Wrote: [ -> ]mypackage
(Oct-22-2021, 10:51 PM)snippsat Wrote: [ -> ]Look at Entry Points
The answer was to type info python
This gives the syntax as python3 -m mymodules.myapp datafile.dat
Which will
  • Run the package rather than attempting to find a program in the current directory.
  • Find the datafile in the current directory.
(Oct-23-2021, 04:44 PM)ChrisOfBristol Wrote: [ -> ]The answer was to type info python
This gives the syntax as python3 -m mymodules.myapp datafile.dat
Then the point of making a package is somewhat gone if do it like this.

So before talk about Entry Points to make if for command line,
if want to load file in a package there is importlib.resources(New in Python 3.7)
So eg in Packaging Python Projects if i add this.
from importlib import resources

def add_one(number):
    return number + 1

with resources.open_text('example_package', 'somefile.dat') as fp: # Just 42 in somefile.dat
    number = fp.read()
Then can now use number from the package.
>>> from example_package import example
>>>
>>> example.add_one(2)
3
>>> example.add_one(int(example.number))
43
>>>