I run Python program which uses couple of source paths on runtime.
I put these rows on my /.bashrc file:
source home/raphael/kaldi/aspire/s5/cmd.sh
source home/raphael/kaldi/aspire/s5/path.sh
So when I'm running from terminal everything works fine and Python manage to locate paths.
However when I'm trying to run through PyCharm for DEBUG purposes mostly it seems that PyCharm can't locate the paths.
Is there anyway to add the paths manually for PyCharm or make it read /.bashrc file. What I am missing?
You can try using the options available in the Run/Debug Configuration settings (Run > Edit Configurations...)
You can set environment variables individually (such as $PATH), or at the bottom is a section to define external tools (scripts) to be run when your Python code is run or debugged. From that sub-section, you could set your bash scripts to be run each time you start debugging.
Alternatively, see if using os.environ would work for your project. Check the docs for more information.
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 configure Pylint as an external tool in my entire project directory for a Python project that I'm working on. I've tried to use the repository as a module with __init__.py and without, and it's not working either way.
I'm having difficulty setting up Pylint to run with PyCharm. I know that I should be running it as an external tool, however the settings confuse me.
The authoritative source on their documentation is broken, so I can't check that up either.
You can set up Pylint to work with PyCharm by following the following steps:
Install pylint:
$ pip install pylint
Locate your pylint installation folder:
$ which pylint # MacOS/Linux
/usr/local/bin/pylint # This is just a possible output - check yours
<!-->
$ where pylint # Windows
%LocalAppData%\Programs\Python\Python36-32\Scripts\pylint.exe # Possible location
Open the PyCharm settings window with menu File → Settings, then navigate to menu Tools → External Tools in the sidebar. (Or search "external tools")
Set up an external tool by clicking on the + sign and filling in the fields accordingly. In Program use the path you got when running which pylint. For the other values, you can use the same from the image.
Run pylint from menu Tools → External Tools → pylint:
Look at your output in the PyCharm terminal
For more details, refer to Pylinting with PyCharm.
If you want to use Pylint to check your whole project or a particular file or directory, you can right click on your project root, file or directory, then activate External Tools → pylint as shown below.
Because I didn't find a working ready-made setup, these are the settings I use in PyCharm CE 2018.1 on macOS:
1 - pip install pylint in your project virtualenv or globally
2 - Add new external tool and configure:
Program: pylint
Arguments: "--msg-template='{abspath}:{line:5d},{column:2d}: {msg} ({symbol})'" --output-format=colorized "$FilePath$"
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$:\s*$LINE$\,\s*$COLUMN$:
Notice the required double quotes for the msg-template, and the escape chars for the output filters. The output filter allows to click on the file path and open the location in the IDE source editor.
Only missing feature would be the output filters to plot the lint descriptions directly into the source view, as is done with the builtin linter. No such feature at this time though.
You can try this Pylint PyCharm plugin:
It provides both real-time and on-demand scanning of Python files with Pylint from within PyCharm/IntelliJ IDEA.
Once you have it installed, the real-time inspection works automatically.
For the on-demand you have several options that go from just checking the current open file to scan the entire project:
(Just for the sake of transparency, I am the developer.)
I now use and recommend the PyCharm plugin which didn't exist when I first wrote this answer.**
A colleague pointed me towards pylint-pycharm on GitHub. It's a wrapper around Pylint with output formatted for PyCharm.
Here's how I set it up:
git clone https://github.com/perses76/pylint-pycharm.git
cd pylint-pycharm
python setup.py build
This creates build/scripts-2.7/pylint-pycharm
Then, in PyCharm, create a new External Tool with these settings:
Program: path to your installation of pylint-pycharm
Arguments: --virtualenv=$PyInterpreterDirectory$/.. $FileName$
Working directory: $FileDir$
Output filters: $FILE_PATH$\:$LINE$\:$COLUMN$\:.*
Now run it from menu Tools* → External Tools → PyLintPyCharm. Each line of output will be hyperlinked to the relevant position in the source code.
A note on the previous answers.
I was searching for a method to make PyCharm aware of the output syntax so I can directly jump to the file locations. That works without using additional tools.
Pylint can be configured to output messages in a specific format using the msg-template option in the pylintrc file or the CLI option --msg-template.
I set it to:
msg-template='{abspath}:{line}:{column}: {msg_id} {msg}'
In the External Tools settings, the Output filters: can be set to
$FILE_PATH$:$LINE$:$COLUMN$: .*
so PyCharm shows links to directly jump to the reported locations.
This can be combined with output-format=colorized so I get this:
PyCharm does not recognize the column despite having it configured. But having the file and line is enough for me.
At first install Pylint with pip:
pip install pylint
You have to open “Settings → Tools → External Tools“ and press the “+” button at PyCharm.
Here are an example with good settings.
Roberto Leinardi has created a Pylint plugin for PyCharm which really works and integrates well into the IDE:
Easy to install from the repositories, full instructions under:
pylint-pycharm
I have a short, yet a happy, experience with it so far! Kudos to Roberto.
Thanks to information here, and updated documentation from PyCharm, I've been able to get this to work nicely to also use the virtual environment for the project (ensuring that any packages can be deployed within the virtual environment and do not need to be deployed globally).
Taking what lkraider provided earlier, but with slight modifications:
Ensure you install Pylint within the virtual environment. Note, make sure that when you created the virtual environment you did not select "Inherit global site-packages". If you do then Pylint will end up being globally and this will not work.
Add a new external tool and configure. This is slightly different compared to what lkraider provided. For one I wanted it to look more like normal Pylint output, hence my msg-template (and Output filter) is a bit different. Each to their own.
The second change is the more critical one for executing Pylint based on the virtual environment. That is the program parameter where I use $PyInterpreterDirectory$.
Program: $PyInterpreterDirectory$/pylint
Arguments: "--msg-template='{abspath}:{line:5d}:{column}: {msg_id}: {msg} ({symbol})'" --output-format=colorized "$FilePath$"
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$:\s*$LINE$\:\s*$COLUMN$:
External tool for Pylint
I am using PyCharm 5 to run a Python 2.7 (Anaconda) script in Ubuntu. My script imports a module with import tensorflow, but this causes the error ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory. So, it seems that the library libcudart.so.7.0 is needed by this module, but it cannot be found.
Now, I have seen that this library is on my machine in /usr/local/cuda-7.0/targets/x86_64-linux/lib. So, in PyCharm, I went to Settings->Project Interpreters->Interpreter Paths. This had a list of paths, such as /home/karnivaurus/Libraries/Anaconda/python2.7. I then added to this list, the path mentioned above which contains the required library.
However, this did not fix the problem. I still get an error telling me that libcudart.so.7.0 cannot be found. If I run my script from the shell though (python myfile.py), then it runs fine.
How can I tell PyCharm where to find this library?
I have noticed that if I have print sys.path in my script, the paths it prints out are entirely different to those in Settings->Project Interpreters->Interpreter Paths... should they be the same?
I came across this problem just recently using a remote debugger, however I believe it's still the same solution. I just added the following to the Environment Variables section in the Run/Debug Configuration options found in Run > Edit Configurations... dialog: LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
The path to your cuda library seems strange to me. I would expect it to be /usr/local/cuda-7.0/lib64 or /usr/local/cuda-7.0/lib.
Did you follow all of the cuda installation procedure?
If you type env on the command line, do you see a path to cuda in your LD_LIBRARY_PATH?
Update from comments below:
The issue is that PyCharm was invoked from the desktop, and wasn't getting the right environment variables. Solution is to either:
invoke from the command line,
create a script to set environment and then invoke, and make a link to that script on the desktop,
or set environment variables on the desktop item
While some of these answers are correct, and could work, I haven't seen what the OP specifically asked for, and that is where to set environments for the python console. This can be accomplished inside pycharm at:
File > Settings > Build,Execution,Deployment > Console > Python Console
In the options there, you'll find a place to define Environment Variables. Set LD_LIBRARY_PATH there.
Edit your pycharm.desktop, specify the environment variable in exec, like below:
[Desktop Entry]
Version=1.0
Type=Application
Name=Pycharm
Exec=env LD_LIBRARY_PATH=:/usr/local/cuda/lib64:/usr/local/cuda/lib64 /home/cwh/software/pycharm-2016.1.4/bin/pycharm.sh
Icon=/home/cwh/software/pycharm-2016.1.4/bin/pycharm.png
Name[zh_CN]=Pycharm
so pycharm will find cuda
Have you selected the right python interpreter in your project's settings?
See here.
I had a similar issue and changing the interpreter solved it without having to create a new icon.
The following works for me on Community edition 2019.3
To set globally for a project:
Open File/Settings/Project/Project Interpreter
click on the cog icon next to the interpreter
choose show all
click on the little folder with tree icon bottom right
add the path to "Interpreter Paths"
I have a python application that is trying to load some Java libraries (specifically Axis2 web services). When I add the necessary jars in Eclipse via PyDev Project Source Folders, everything seems to work fine. However, I want to be able to do this at run time by adding to sys.path, but then my application doesn't seem to work.
In both cases I can load the jars just fine, but something must be different for there to be different results. My question is, is there a difference between adding jars via the sys.path at run time with sys.path.append() versus passing -D to the jython interpreter?
The problem turned out to be a difference in the way that Eclipse starts up the Jython interpreter as opposed to starting Jython manually from the command line. In the Eclipse Run Configuration pane, there is a way to see the command that is used to run your application. Mine looked like this:
/usr/lib/jvm/java-7-openjdk-i386/bin/java
-classpath /usr/local/lib/jython2.5.3/jython.jar:...
org.python.util.jython
-Dpython.path=...
myScript.py
(Note: I added line breaks for readability)
So it seems that Jython is launched from Java and that the Java classpath had to be fed the paths in addition to just the python path.