Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyPI package and files
#1
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]
Reply
#2
mypackage was created by you? If so then it should be in the same folder as myprogram.py
Reply
#3
(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.
buran likes this post
Reply
#4
Smile 
(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.
Gribouillis likes this post
Reply
#5
(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
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Including data files in a package ChrisOfBristol 4 2,465 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  fileinput package appears to be zeroing files rexrf 0 1,441 Jul-01-2020, 06:05 PM
Last Post: rexrf
  Updating a package published on PyPI marqrdt 1 2,407 Jan-02-2018, 02:03 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020