Call a bat file from python and import the environment - python

I'm writing a python script to coordinate building of a number of Qt-projects on windows. The python script decides which architecture to build for, and which versions of Visual Studio and Qt to use. After that is done I would like to run the proper qtenv2.bat and vcvarsall.bat to setup my environment, and then import the resulting environment for use in my python file.
If the bat files contained only setting of environment variables this would be easy, but since the also contain logic and stuff I need to actually run them and pickup the changes they have done.
I can solve this by using multiple bat/py scripts, but my goal is to write one python script to take care of this build process.

"If the bat files contained only setting of environment variables this would be easy"
Include a set command at the end of the batch file and capture its output that will be just the listing of the variables in the environment and their values

Related

Can I run a python script in arcpy from a .bat file?

I am able to open ArcGIS from the batch file but in order for the script to run successfully I need to be able to run it off the python console within ArcGIS. I can copy and paste the code into the python console within ArcGIS but I want to be able to have the code entered into the console automatically just from the batch file. I'm using ArcGIS 10.6. Is there some file path I should be using?
Thanks in advance for any suggestions.
this is rather strange, I don't think I understand the problem here. You want to run Python code from a batch file in the Python console in ArcMap/ArcCatalog?
What is it you want to acomplish?
You can create a standalone Python script, load it in the console and run it.
If you want to schedule something you can create a Python script and run it in PowerShell or cmd.
If you want something to run in ArcMap/ArcCatalog you can create a model or a script and again can run this anywhere, put it in a toolbox, schedule it, whatever.
If you're a windows user first of all it's nice to have the path to python.exe (usually C:\Python27\ArcGIS10.x) in your environment variable so you can call it from anywhere. I think PATH should be updated when you install ArcGIS.
When you run Python code in the console you don't need to import arcpy, however you do need to import it if you will run it from the command line for example.
You can find a nice GDB administration example here:
https://desktop.arcgis.com/en/arcmap/10.6/manage-data/geodatabases/using-python-scripting-to-batch-reconcile-and-post-versions.htm
Careful when dealing with absolute paths, then make sure you're running the script in the same directory.
If you want to define a workspace (some GDB, shapefile or a database connection file) you can reference them like this:
myGdb = r"D:\SomeFolder\MyGDB.gdb"
Hope this helps if it's relevant.
Welcome to the SO community. If I understand you correctly, you do want to run python script from ArcGis's console. You would rather run script from your windows/linux shell/cmd, yes?
SHORT: you should python libs of ARCGis, which you should put into PYTHONPATH enviroment variable. If you configure correctly PYTHONPATH enviroment, then you cound run python gis_script.py which utilize ArcGis.
LONG: sometimes you use libraries in python. If you install libraries in your system, before using this libraries, python interpreter should know about these libraries. This configuration is stored in PYTHONPATH enviroment. ArcGis is large python library.
-- edit --
look here: https://community.esri.com/thread/119922 or google: ArcGis PYTHONPATH

how to execute a function each time you run python shell

I want to know how (if there is a way) to write a function and have it automatically defined when you start a python shell, or to keep certain defined functions after the shell closes so you don't have to keep re defining it if you use it a lot.
Yes, you can, you need to set your environment variable PYTHONSTARTUP to a python script you would like to be executed at every python startup.
By convention, such files are named by adding the rc suffix to the program name you're trying to tweak, and are usually located at the root of your home directory. In that case I would create the python file under $HOME/.pythonrc and then run export PYTHONSTARTUP=~/.pythonrc (for UNIX-like systems, it could be slightly different on Windows if you don't have a MinGW or equivalent).
Here's an example of .pythonrc file that you can play with: https://gist.github.com/twneale/5245670

Sharing a common dll/pyd dependencies for several exe in Python

I am successfully using py2exe to produce a Windows binary distribution of several independant Python scripts (using standard Python27.dll + PyQt pyd files + Matplotlib pyd files) producing an executable for each one (app1.exe, app2.exe, ...).
Each of these executables work as expected if kept in the same directory as their dynamic dependencies (these dependencies are exactly the same)
I tried to create a common directory, say CommonDynamicLibrariesPython , which contains all these dll/pyd dependencies, then place each application in a different location and attempt to run it through a batch file after I set the path system variable locally to include the dll/pyd, using something like:
:: run_app1.bat placed in c:\dir_app1
set PYTHONDLL=my_path_to_CommonDynamicLibrariesPython
set PATH=%PYTHONDLL%;%PATH%
start /B app1.exe
Alternately I tried also changing PYTHONPATH from the batch and from the python script itself, but no luck.
Can anyone tell me why isn't this possible ?
In other words, is it possible to run a python executable produced by py2exe when placed outside its original dist folder, without using shortcuts, but using a batch file

Python: runtime shebang problems

Here is the problem I am trying to solve. I don't have a specific question in the title because I don't even know what I need.
We have an ancient Hadoop computing cluster with a very old version of Python installed. What we have done is installed a new version (2.7.9) to a local directory (that we have perms on) visible to the entire cluster, and have a virtualenv with the packages we need. Let's call this path /n/2.7.9/venv/
We are using Hadoopy to distribute Python jobs on the cluster. Hadoopy distributes the python code (the mappers and reducers) to the cluster, which are assumed to be executable and come with a shebang, but it doesn't do anything like activate a virtualenv.
If I hardcode the shebang in the .py files to /n/2.7.9/venv/, everything works. But I want to put the .py files in a library; these files should have some generic shebang like #!/usr/bin/env python. But I tried this and it does not work, because at runtime the virtualenv is not "activated" by the script and therefore it bombs with import errors.
So if anyone has any ideas on how to solve this problem I would be grateful. Essentially I want #!/usr/bin/env python to resolve to /n/2.7.9/venv/ without /n/2.7.9/venv/ being active, or some other solution where I cannot hardcode the shebang.
Currently I am solving this problem by having a run function in the library, and putting a wrapper around this function in the main code (that calls the library) with the hardcoded shebang in it. This is less offensive because the hardcoded shebang makes sense in the main code, but it is still messy because I have to have an executable wrapper file around every function I want to run from the library.
I would change the environment variable PYTHONPATH and also the environment variable PATH. Point PYTHONPATH to your virtual environment and PATH to the directory that contains your new python executable, and make sure the path to your python executable comes first.
I accepted John Schmitt's answer because it led me to the solution. However, I am posting what I actually did, because it might be useful for other Hadoopy users.
What I actually did was :
args['cmdenvs'] = ['export VIRTUAL_ENV=/n/2.7.9/ourvenv','export PYTHONPATH=/n/2.7.9/ourvenv', 'export PATH=/n/2.7.9/ourvenv/bin:$PATH']
and passed args into Hadoopy's launch function. In the executable .py files, I put the generic #!/usr/bin/env python shebang.

bootstrap script to set environments temporarily

I am trying to follow some simple instructions to use python for operating with QGIS from outside the application. Following the instructions in http://www.qgis.org/en/docs/pyqgis_developer_cookbook/intro.html#python-applications I am stocked where it says "These commands can be put into a bootstrap script that will take care of the startup". I never done this before, I have no idea how to create this type of script...
The commands I should bootstrap are:
set PATH=C:\qgispath;%PATH%
set PYTHONPATH=c:\qgispath\python
That is, these are ways to temporarily set system environment variables. However, having both python and (of course) the cmd prompt (my OS is Win7), I have no idea how to create the bootstrap script, and then how to call it from python, can anybody help me?
By a bootstrap script, the documentation probably means a batch file, which initializes the path variables and then runs your application.
For instance, if you have application stored as qgis_sample.py, bootstrap_example.bat would look something like:
set PATH=C:\qgispath;%PATH%
set PYTHONPATH=c:\qgispath\python
python <path_to_application>\qgis_sample.py
Hope this helps!
EDIT:
For running the bat file using a python script, refer to: Run a .bat file using python code

Categories