How to execute a script or a funciton through python setuptoos? - python

I use Distutils to distribute my package, it's very convinient. Now I want to execute a script or a function after the user installed my package through command like pip install mypackage. I thought I could find a API like register(funtionCalledAfterPackageInstalled), just like atexit.register(someFunction), which was often used in python to execute a function before the app exited, but I failed. So I want to ask: is there any elegent way to do such a thing?
Background: I reference a frequently updated dll in my package, and I don't want to put it in my pakcage so I don't need to care about the update. I just want it to be automatically downloaded through a function call after my user install my package. And the environment is WINDOWS, so linux pakcage management tools not work.

After calling setup() in your setup.py file you can call another Python function or method. So you can run python function making what you need or execute external command with subprocess by example.
It will result something like:
from distutil.core import setup
import subprocess
setup( ... stuff ...)
# Call external command
subprocess.Popen('command here', ... attrs ...)
# Or another Python function you wrote
my_function()

Related

Run a Python made CLI without running the .py file from terminal

When you install Git on your computer, it can be run from the terminal with "git". Similarly, Python can be run with just "python" or "py".
If I make a CLI with argparse and Python, is there a way to have users simply install it and use it in the terminal just by typing
$ myCLI -option
Rather than
$ python path\to\myCLI.py -option
Like all the CLI's I use/interact with do?
Crude Solution
Your script just needs to be somewhere in the user's $PATH (and to have no extension if you don't want them to type myCLI.py). Additionally it needs to start with a magic comment #!/usr/bin/env python3 (If you are running Linux) and be executable (on linux chmod +x myCLI prior to distributing it). Alternatively you create a script in the native shell language of the desired OS (so sh for linux) which calls python3 /path/to/script or python -m module or the like.
If you you name your main script without a .py it prevents you importing it from anything else, so the usual approach is to have an 'entrypoint' script which consists of something like:
#!/usr/bin/env python3
from myApp import main
main()
(see the PyPa docs for a discussion)
Better solution
Of course, you need to deploy your entrypoint script somewhere in the end-user's $PATH. This can be more intersting, but luckily the whole thing is handled by setuptools, and you don't even need to write a script: you just need an entry point function:
Use console_script entry points to register your script interfaces. You can then let the toolchain handle the work of turning these interfaces into actual scripts
official docs
you just put in your setup.py:
setup(
entry_points = {
'console_scripts': [
'PKG_NAME = package.module:func',
],
}
)
and anyone who types PKG_NAME will effectively run package.module:func() (so func is your entrypoint main() function above).
See this question for a discussion of the merits of this approach over the old approach of writing separate scripts for each platform.
Note that if you're not building your app as a python package, but are using some other packaging standard (archlinux PKGBUILD or .deb or whatever) you can still take the same approach---just write the entry point script as above and have the package place it somewhere in the user's $PATH.

How to run a Python Module directly in Ubuntu Terminal?

I want to make and launch a command interpreter in Python for Linux. I've used Python's cmd class to run the program.
Currently, I run the program by executing
python main.py (where main.py is the main project file).
I want to be able to run my project by just typing in my project name into the terminal and publish this project on PIP. How do I do that?
EDIT
Im aware that there are multiple examples on the web which demonstrate how to execute a python file directly by filename (adding Python shebang at start of file).
It does answer first half of my question, but I also intend to publish this package on PIP. All tutorials I read on publishing packages on PIP direct me to make python modules which can be run in other python programs, something I dont want. I want to make a python package which can be run directly on
If you package your package with setuptools (which is the most modern way as far as I know), you can just add entry_points to your setup.
Example: You want a script calling the function bar() in the module foo and the script should be called via the terminal with the command bar, then your setup will look like:
setup(
...
entry_points = {
"console_scripts": [
"bar = foo:bar",
],
},
...
)
This is more or less well documented here.

Module ImportError in running Python Script in JMeter

I would like to use python scripts in Jmeter, so I can check the Load and the performance of an application...for that I've created a Thread with JSR223 Sampler, and I put the content of python script, and after the run, Jmeter throws error like : File "", line 8, in
ImportError: No module named opcua, ModuleNotFoundError: No module named 'dateutil'
see snapshot:
I don't know how to install this modules (package)in Jmeter, with PyCharm, I installed the package in the Project Interpreter under the setting see the snapshot...
You can do something like:
Install required module(s)
jython -m pip install foo
Add the next line(s) to the beginning of your script:
import sys
sys.path.append('\\location\\of\\jython\\site-packages')
However from performance perspective it will be much better to re-write your script in Groovy as calling Jython interpreter is not the best choice when it comes to high loads.
Also be aware that you can invoke Python interpreter as "external" process using JMeter's OS Process Sampler (if amount of the existing Python code is too big or too complex to rewrite in Groovy), see How to Run External Commands and Programs Locally and Remotely from JMeter for more details.

creating a makfile to run simple python program on different files

I have a python file that needs to import 5 other python modules in order to run.
The main program is very simple and short because it uses the classes defined in the 5 import files. Now I have to submit a make file that responds to several commands. All the commands run the main program on different files and are instructed to be as follows; "make cat-dictionary" should display the file "dictionary" that the program will be run on.....while "make run-dictionary" will execute the python program on "dictionary". I found a ton of help on how to use make file with C code that needs compiling. But I am confused how to make sure my make commands allow the main python program to import the 5 modules it needs before executing it? Do I have to use a command that prepares the modules or will my python program just import them automatically if they are there.
Packaging Python program via setup.py
To make Python code easily installable, you shall use setup.py, which will allow creation of a package.
Python make via doit
If you really need to run multiple dependent tasks and search for Python alternative to Makefile, check doit package.
Note, that for completing a python program doit is not needed.

How to import user-defined modules (packages) properly when calling a python script through Windows Task Scheduler?

I am using a Windows server (2012)'s Task Scheduler and Python 2.7
I am scheduling a task to be run using python. Specifically: C:\Anaconda\python.exe and calling a script as the argument located in another directory.
However, all my import statements from that script do not get imported. I tried a bare minimum script using only standard libraries and all was fine (i.e. the script worked flawlessly and ran as intended). However, when I start importing my own user-created packages (located in the same hierarchy but different folders as the script), they fail to import. I think I am misunderstanding something about how to import packages in Python.
My user defined packages are imported at the start of the script, like so:
import yolo
import wisconsin
When I run the script in Eclipse (or whatever editor I am using), the import statements work fine. This incident is isolated to running the script using Task Scheduler in Windows. Can anyone surmise what the problem may be? Do I need to be more explicit with my import statements like maybe including a path somehow?
Let me know if something is unclear.
Could it be that when I run the script using Windows Task Scheduler, it isolates the script in its own environment, whereas if I run it in Eclipse, I have all my packages in the same environment? How would I fix this if I intend to schedule a script that relies on these packages?
Can you try to put imports under __name__ part and use it in it?
Like this:
...
if __name__ == '__main__':
import yolo
import wisconsin
# use yolo here
...

Categories