![]() |
PyPI package and files - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: PyPI package and files (/thread-35343.html) |
PyPI package and files - ChrisOfBristol - Oct-22-2021 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] RE: PyPI package and files - Axel_Erfurt - Oct-22-2021 mypackage was created by you? If so then it should be in the same folder as myprogram.py RE: PyPI package and files - snippsat - Oct-22-2021 (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. RE: PyPI package and files - ChrisOfBristol - Oct-23-2021 (Oct-22-2021, 08:49 PM)Axel_Erfurt Wrote: mypackage (Oct-22-2021, 10:51 PM)snippsat Wrote: Look at Entry PointsThe answer was to type info python This gives the syntax as python3 -m mymodules.myapp datafile.dat Which will
RE: PyPI package and files - snippsat - Oct-23-2021 (Oct-23-2021, 04:44 PM)ChrisOfBristol Wrote: The answer was to type info pythonThen 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 >>> |