How to execute python code from within a python method? - python

I'm writing a deployment engine for our code.
I plan to put a "deploy.py" in each project directory and then decide what projects need to be deployed, having the "deploy.py" of each project execute his logic for proper deployment configuration.
How would you recommend doing this? I've been thinking about "executing" the python code in each deploy.py file from within the "deployment_engine.py" code (I got this idea from scons method of doing stuff).
Alternativly I would like to somehow "import" the code (note that under each project the file is named "deploy.py" so a simple import deploy.py doesn't seem to do the job here)
Thank you,
Maxim.

You can use the __import__ function to do dynamic imports.

Related

Is adding project root directory to sys.path a good practice?

I have a question about adding project path to python, for facilitating import effort.
Situation
When I write code in Python, I usually add necessary path to sys.path by using
import sys
sys.path.append("/path/to/dir/") # almost every `.py` need this
Sometimes, when my project gets bigger with many levels of directories, this approach seems bulky and error-prone (especially when I re-organize my files)
Recently, I start using a bash script (located at project root directory) that adding the sys.path.append with project root argument to .py file in the project. With this approach, I hardly have to manually care about importing a module.
Question
My question is: Is that a good practice? I find it convenient for myself, compared to my old method, but since the bash script is a separated file, I need 2 command to run any script in my project (one for the bash and one for the .py). I can include the command calling .py to the bash, but it far less flexible than directly call it from terminal.
Really want to hear some advices! Thanks in advance. Any suggestion will be gratefully appreciated!
It is generally not good practice to use manipulate sys.path within a python library or program. You should add the relevant paths to the PYTHONPATH in the calling environment for your python program:
PYTHONPATH="/path/to/other/projects/directory:$PYTHONPATH" python ...
or
export PYTHONPATH="/path/to/other/projects/directory:$PYTHONPATH"
python ...
This allows you to easily manipulate the paths that your program or library will search for dependencies easily without modifying your code.
It is also very easy to manage this in your personal development environment by modifying your bashrc or in your production environments in your init script (or other wrapper script) and provides you with one place to update each time you add or modify your project paths.
Given that you mention that you have almost one directory per .py file, you should also consider how your code might be organized into packages to further simplify your setup.
It's not a particularly good practice, though you could get away with it. Better to look into virtualenv though (or pipenv) for a smoother workflow.

Defining and using a Python class within a ROS catkin workspace

I am running ROS Indigo. I have what should be a simple problem: I have a utility class in my package that I want to be callable from our scripts. It only needs to be called within our own package; I don't need it to be available to other ROS packages.
I defined a class named HandControl in a file HandControl.py. All my attempts to import it, or use it without importing, fail. Where in the catkin workspace do I put it -- the root of the package, or in scripts? Do I need __init.py__ anywhere (I have tried several places)?
It is a good practice to follow the standards of Python and ROS here. Scripts are typically placed in /script directory and they should not be imported into other python scripts. Reusable python code is an indication of a python module. Python modules should be placed in /src/package_name and there you should create __init__.py as well. This module will be available everywhere in your catkin workspace. There is a good chance this structure will help you in the future to structure things, even though you may not seem to need it at the moment. Project typically grow and following guidelines helps to maintain good code. For more specific details checkout this python doc.
Erica,
please see this school project, which was written in Python and run on ROS Indigo. If you'd look in the /scripts folder, you can see an example of a custom class that is being called from other scripts. If you'd look into the launch files in /launch you can see an example of configuring the ROS nodes - maybe that is your problem.

Map all files and objects of a django Project

Currently, I'm starting to work on a Django project which it's already begun.
Don't misunderstand me, I already know how basic files and dependencies work and relate to each other on this framework (forms, views, models, urls, templates, etc)
But my problem is, that this is a project with lots of files, classes, models, constants, etc... And has lots of code not written by myself.
I'm not very familiar to these internal custom objects, so, I need a module or script on python which can read all folders and map all relationships and dependencies (internal project objects, not modules or requirements), so I can better understand what's already done and work more efficiently on these modifications I have to do.
Currently I'm studying it manually, but it's very unefficient, and a slow process, so, if anyone knows about a tool or maybe a script, of these characteristics, please let me know where I can find it, download it, etc...
Thanks in advance!
Python has a builtin module called modulefinder that can be run from the command line to trace dependencies. It can be invoked like
python -m modulefinder mymodule.py
to print module dependencies of mymodule.py

How can I use "include.yaml" in google appengine to share a library within two apps?

I have two different applications on GAE, but both have some code in common.
I wanted to share that code but I can't find a way to import a .py file that's not in the same directory as the main app.
I think that "includes" in the yaml file could be what I need (https://developers.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Includes) but I can't find any example on how to use it.
I understand I need an include.yaml file, but what should I write in it!?
Is there some more documentation I'm missing?
I can't find a way to import that code in my main app... is there any?
Thats not necessary.
Make sure you include the path to the file with sys.setpath() pointing to the directory containing the other python code, and put an __init__.py in that directory.
The best place to do this is in appengine_config.py

Where to put standalone-django scripts without breaking imports?

I'm still pretty new to Python.
I have a Django website with several apps and a /libs directory. I need to add a couple cron jobs that will use my Django models. I've already worked all that out, no big deal.
I have a problem with my imports, though.
I would like to include these scripts in the App they generally belong-to. But when I execute the script from the commandline, it's unable to load modules from the /libs directory.
To illustrate specifically the dir structure, imagine this pseudo-code:
import ./../libs/mysharedlib.py
In other words the scripts and sharedlib are:
~/project/myapp/myscript.py
~/project/libs/mysharedlib.py
I get that I could just add ~/project to my PYTHONPATH but then I have to worry about doing this during deployment and it just feels broken.
Should I move my scripts out of my apps and put them in the ~/project? Is there a better way?
There is a better way, custom management commands solve this.
https://docs.djangoproject.com/en/dev/howto/custom-management-commands/
These let you write stand alone utility scripts. You can run these as a cron or just as utilities. They use the exact same paths as any other module in your django app.
While these solve import problems does your libs directory have a __init__.py file? Can you import your lib directory in your views? Or is your import just not working in your cron scripts? Either way custom managment commands address this.

Categories