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
Related
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
I'm setting up a Django project for the first time using Pycharm, and I'm trying to figure out what the configurations do.
I have looked online at the documentation, but it seems to be more focused towards people who already know what configurations do.
I'm assuming it is used to set up my localhost, however I'm unsure. Any help would be greatly appreciated!
In an informal way, we can define PyCharm configurations as a comfortable way to start a new process.
Suppose that you have a Python script called main.py. If you have to run it via command line you will type
python main.py
In the same way, PyCharm offers the possibility to run this script using configuration. It's sufficient create a new Python configuration and setup the script path (location of main.py) and the relative python interpreter.
In fact, can happen that some scripts must be run using a virtualenv or a specific enviroments. For this reason, at the creation of PyCharm configuration we need to specify also python interpreter.
Configurations are not used just to start python scripts, but it's possible to create different types.
As you pointed out in your question, it's possible to create configurations also for Django.
A typical usage in this case is to create a configuration to start the runserver.
But this is not all, because it's possible create configurations also for django commands, for migrations and also for Unit Test.
Pycharm configurations are useful because in this way, no need to setup every time all environment to start a process, but you need just to create configuration and save it. You can use it whenever you want.
Last but not least, using configurations allow you to debug your code easily.
Configurations can be started also in debug mode.
PyCharm documentation
When you run your code/project in PyCharm, maybe you need to specify some set of environment variables, the entrypoint is a particular script in your project, maybe there's some other script you always need to run first before you can run your project, or lots of other fiddly things that are a pain to type in every time.
The purpose of PyCharm "Run Configurations" is to let you save all these kind of things into a single method of running your project with a single click or shortcut.
Django Configurations are a type of Run Configurations with a few extra Django-specific config options thrown in. The default settings in the Run Configuration are sensible (running on localhost:8000), although you will need to make sure you specify the location of your Django Settings File.
How I config Run/Debug Configurations in Pycharm Community:
Add a Python Configuration
Script path: "path to manage.py"
Parameters: runserver "host":"port" "option like '--noreload', '--nothreading'"
Python interpreter: "path to venv"
Now you are able to run with Shift + F10
In order to pycharm general setting left-top File>Setting
screenshot..
enter image description here
Python Setting....
Open a project or import
After open or import you view project name option in rigth-top bar like this
enter image description here
on click you can view configuration option. click it and view this form for all python or other environment setting.
enter image description here
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
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
I'm doing small time project development using PyCharm. I use Pycharm for its intellisense features. As I develop each piece of code, I like to run it occasionally to test it. All I need at the point of development is to be able to run the file. However, when I right click and try to run a standalone file, PyCharm tries to be intelligent and shows me options to run my code with unit-tests and other fancy testing gimmicks. I don't want to deploy any testing framework at this point.
All I want is to be able to run any file as it is. But somehow, PyCharm is not allowing me to do that for every file.
I will appreciate if someone can provide a workaround for this. I'm using Python 273
You can simply choose the 'Run' entry in the context menu for the file in the Project view:
This will run the file exactly as if you had entered
python
from the command line, in the directory containing the file.
The first time you do this an entry is also added to the toolbar, which allows you to run the file with a single click
and provides options for customizing a range of options supplied automatically each time you run.
I just came across the same problem. It was because it had a class called TestClass in the file. I changed the name of the class and then I was able to run the file as normal.