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

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

Related

python is not recognized as an internal or external command, operable program or batch file

I realize that a lot of people have had this question but I've tried their solutions and they haven't helped me. I watched this video here but his solutions is not helping me:
fix problem
I can get python to work on the python app and using it the path of the python interpreter is
import sys
print (sys.executable)
>>> C:\Users\Owners\Documents\Python
I then set the environment variable like so:
I've also tried attached 'python.exe' but that didn't work either. When I then write python in the terminal I get the error message in the terminal.
You should make a new environment variable in your system variables named PATH if it doesn't already exist then you need to add your python interpreter path to that variable.
Adding Python.exe file in Path won't work as it is just an executable file, also which version of Python have you installed because according to this python 3.9.0 and above won't run on Windows 7 or earlier.If this is not applicable in your case then click on New in your system variables menu and add PATH as name and locate the Python Application Path and Python Scripts Path in folders (i.e where you installed Python) and add it to the newly created PATH

"python.exe can't find '__main__' module in ..." Error when trying to run python script opencv_blink_detect.py in virtual environment,

Apologies if there's missing information. I'm using python for medical research and am trying to run a program to count the number of blinks via video footage.
https://github.com/skvrahul/blink_detect
The script is located here
My virtual environment is set up here:
Package install instructions were followed from here
Terminal open via Anaconda
Attempt to run via readme instructions
Attempt to direct terminal to look at file path
As you can see, the error message is
(opencv-env) C:\Users\wmj>python C:\Users\wmj\Documents\Python Scripts\Biometrics\blink_detect-master.py -p -sp.dat
C:\Users\wmj\AppData\Local\Continuum\anaconda3\envs\opencv-env\python.exe: can't find 'main' module in 'C:\Users\wmj\Documents\Python'
The contents of C:\Users\wmj\Documents\Python is just Anaconda3-2018.12-Windows-x86_64
I have searched online and most solutions to this problem seem to be either solved by modifying the .py file or doing something with Pycharm
What is the solution to this? I want to be able to run the script as a demonstration to my supervisor
Thank you,
WW
Try first to cd to the exact location of the package and then run exactly as written in the readme instructions.
Python has to get the full path of the script you're trying to execute and the script itself has to get the sp.dat file as an argument.

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

Load iPython with custom packages imported

Does anyone know if it is possible to load ipython preloaded with custom packages please?
I'm running Python 2.7 on Windows 8.
When I load a DOS prompt, I run ipython preloaded with pylab by typing
ipython --pylab
I've managed to create a shortcut to open a DOS prompt with this automatically fired, thus effectively creating a shortcut to iPython.
However, I'd like iPython to start preloaded with some of my custom packages. So I wonder if there is a way to start iPython and automatically execute the following lines, say:
import package1 as my_package
import package2 as my_second_package
I've had a look online and there's some information on "magic" commands and scripts in iPython which looks like it might help, although I wasn't sure how to use this.
All guidance welcomed.
Many thanks.
What you want is a startup script.
First run ipython locate profile to find the profile folder. Then find a startup folder in there. Create a .py file (any name) in the startup folder with the imports you want, and IPython will run that whenever you it starts.
If you have a look at the documentation, you will find out that IPython will run whatever is inside the file pointed by the PYTHONSTARTUP variable.
Create one, export the variable, and there you go.
References:
http://ipython.org/ipython-doc/stable/interactive/reference.html#ipython-as-your-default-python-environment
https://docs.python.org/2/using/cmdline.html#envvar-PYTHONSTARTUP

Categories