As you can see in this link, you can use:
C:\> assoc .py=Python
C:\> ftype Python="C:\python27\python.exe %1 %*"
I suggest you to see the link I put before. If you want to run it as a package like pip install egscript and then use egscript, you have to create a package to upload it into the Python Package Index(PyPy). To make that posible, follow the steps in these links: Build Your First pip Package, Packaging Python Projects.
Convert python script in module - Stack Overflow
How do I package my python code into an executable I can send to my friend who does not have python?
java - Convert a python file to a library - Stack Overflow
Turn python script into a function - Stack Overflow
As you can see in this link, you can use:
C:\> assoc .py=Python
C:\> ftype Python="C:\python27\python.exe %1 %*"
I suggest you to see the link I put before. If you want to run it as a package like pip install egscript and then use egscript, you have to create a package to upload it into the Python Package Index(PyPy). To make that posible, follow the steps in these links: Build Your First pip Package, Packaging Python Projects.
You could set an alias in your .bashrc or .zshrc file
alias egscript="python3 /path/to/egscript.py"
Or you could turn it into an executive using something like pyinstaller, and then you can run the script like ./egscript
Β» pip install stickytape
Hiya thanks for reading! I followed a tutorial and built a tiny game with pygame. I am trying to wrap it up in a single executable file I can send to a friend, such that they won't have to install anything to run it. I have unsuccessfully used p2exe and pyinstaller. Is there a standard way to package python applications? I can share my code or the tutorials/links I am looking at if that is desirable. In gratitude, Prathmun
Pls have a look and try at this article: how to create python library. They are writing very clear and in details I think you can create your library in 4 hours :)
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f
If you want to convert your python file into an API for anyone to use the easiest way is to use Flask.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
With a basic structure like this you can run your server/API and when you query http://127.0.0.1:5000/ you'd get "Hello World" response.
You can find Flask documentation here: https://flask.palletsprojects.com/en/1.1.x/quickstart/
And since you want to use AWS Lambda moving forward here is a guide how to create an API using Flask and AWS Lambda: https://www.serverless.com/blog/flask-python-rest-api-serverless-lambda-dynamodb
The common usage is to always declare functions (and/or classes) in a module that can be used by other modules and to add at the end a if __name__ == '__main__': test to directly exec something if the script is called directly.
In your example, it would give:
# morning.py
def morning_func():
print 'Good morning'
if __name__ == '__main__':
# call morning func
morning_func()
That way, you can execute it simply as python morning.py or include it in other Python files to call morning_func from there
Adding to above answer, the best way in this case would be to go through the original morning script line by line and wrap the actions in related functions. Refer this recipe on How to convert a Python script to module.
This feels like an RTFM thing, but man, I've been googling and it's hard to tell whether any particular result tells me how to do the thing I want, or just some other thing that I guess more people want to do.
I have a python script that I want to be able to run from anywhere on my filesystem by calling p9. Until now I've been doing that just by symlinking from somewhere on my path to the folder where I'm developing it.
It requires the module plotnine. Previously I just installed that with pip, but at some point pip on arch stopped letting me install things globally. I get it. I'm tempted to just do --break-system-packages, I think it'll be fine, but I'm gonna try to do it right. But I'm not sure what the way to do it right is.
-
I can create a venv and install
plotninein that? Then I think I have to be inside thatvenvevery time I run the script. (Or change the #! line to point inside the venv, but then I can't share the script.) -
I can turn the script into a package that can be uploaded to pypi? I'm not entirely sure how to do that (this tutorial doesn't mention executables). Once I've done that, how do I install it on my local machine? Can
pipxinstall from here without me having to send it to pypi first? And, when I'm developing, can I test changes without installing to my local machine? (I'm fine with entering a venv for this, so I expect this works.) -
Something else?
The second approach feels promising, but I've reached the point where I'm turning to help rather than trying to keep going myself.
Rather than using setuptools non standard way of proceeding, it is possible to directly rely on distutils setup's function, using the scripts argument, as stated here: http://docs.python.org/distutils/setupscript.html#installing-scripts
from distutils import setup
setup(
...,
scripts=['path/to/your/script',],
...
)
It allows you to stay compatible a) with all python versions and b) not having to rely on a setuptools as an external dependency.
@Zach, given your clarification in your comment to @soulmerge's answer, it looks like what you need is to write a setup.py as per the instructions regarding the distutils -- here in particular is how you register on pypi, and here on how to upload to pypi once you are registrered -- and possibly (if you need some extra functionality wrt what the distutils supply on their own) add setuptools, of which easy_install is part, via the instructions here.
Programs that can do what you ask for are:
- PyInstaller: http://www.pyinstaller.org/ [Windows, Linux, OS X]
- cx_freeze: http://cx-freeze.sourceforge.net/ [Windows, Linux]
- py2exe: http://www.py2exe.org/ [Windows]
- py2app: http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html [os x]
But as mentioned you can also create a Package with Distribute and have the other packages as dependencies. You can then uses pip to install that package, and it will install all of the packages. You still need to install Python and pip, though.
cx_freeze will append your python scripts to a standalone Python loader and produce a directory containing the program, and shared library dependencies. You can then copy the resulting distribution to other machines independent of Python or your modules.
$ cat hello.py
print "Hello, World!"
$ ls dist/
datetime.so _heapq.so hello libpython2.6.so.1.0 readline.so
$ cat hello.py
print "Hello, World!"
$ cxfreeze hello.py
... <snip> ...
$ ls dist/
datetime.so _heapq.so hello libpython2.6.so.1.0 readline.so
$ ./dist/hello
Hello, World!
A better answer may be to create a PIP package that identifies these third modules as dependencies, so installation can be as simple as "pip install mypackage; ./package"