How to import ros to PyCharm - python

I am developing a robot using the ros framework. As ide, I use PyCharm. But I can’t import ros into it. On the ros site there is the article about ide http://wiki.ros.org/IDEs. There is information about using ros with pycharm. I have to modify the .desktop file, but I installed PyCharm using a snap from the software center. Where can I find the .desktop file for snap applications? Can there be another method to import ros into PyCharm?
Edit:
#hug Yes, I launched this command here is the result
/snap/pycharm-community/103/meta/gui/pycharm-community.desktop
/snap/pycharm-community/103/snap/gui/pycharm-community.desktop
/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop
/var/tmp/pycharm-community.desktop.swp
I think that the .swp file has remained since the last reinstallation of PyCharm. I reinstalled when I tried to do it myself and realized that I had broken the program.

If you want to use from the auto-suggestion with ROS packages in Pycharm IDE, you could do the following instructions:
File > Settings (or Ctrl+Alt+s as shortcut)> Project: > Project interpreter.
In the project interpreter dropdown list, you can specify ROS Python interpreter by selecting the appropriate from the list.
[NOTE]:
You could add a virtual environment with the above instructions, then you should add ROS distpackages (roslib) on it with this instruction.
ROS distpackages path that you need: /opt/ros/kinetic/lib/python2.7/distpackages

Follow these video instructions for setting up pycharm with ROS.
https://www.youtube.com/watch?v=lTew9mbXrAs
Debugging, code completion, imports, etc all behave correctly. You can do all your python ROS development entirely in pycharm.
In summary:
Create a virtual environment virtualenv venv --system-site-packages
Source your virtualenv and your ROS workspace and launch pycharm from this (sourced) terminal. (enable Shell Scripts in the JetBrains Toolbox to allow command-line pycharm)
Under the project structure you will "Add Content Root":
Keep [ros_workspace]/src (which should already be there). Mark all the the src folders containing python code as "Sources".
add /opt/ros/noetic/lib/python3/dist-packages
add [ros_workspace]/devel/lib/python3/dist-packages. Exclude any packages in [ros_workspace]/devel/lib/python3/dist-packages that contain source code (e.g. are not just message packages). If you have packages that contain both source and messages, you will need to separate these for pycharm to resolve imports correctly in the editor
More details and explanation in the video

If anyone else is struggling with getting PyCharm intellisense and autocomplete to work with your own ros python packages, this answer was very illuminating.
Basically PyCharm does not dynamically interpret the devel/ space __init__.py for your packages, so at runtime the packages are available, but no intellisense in PyCharm. Changing the Project Structure settings to add your <package>/src as a Source folder solved the issue for me. Bit of a pain, but hey, intellisense is back!

Related

How can I use Libreoffice embedded python in a Venv from Pycharm

I am trying to use Pycharm to develop Libreoffice python Macro with in the virtual environment with not a lot of success. You'll find below a few things I have tried with the differnt errors I have had so far. I welcome any answers on the errors below as well as alternate solutions.
first try : set up directly the venv environment
As when you work with Libreoffice, you need to use the embedded python integrator, the first attempt is when defining the python integrator in pycharm, I reference this embedded interpreter (located somewhere in Program Files\Libreoffice\program` on my windows machine, I click the new venv box in pycharm and get the following error :
Error: standard python 'venv' module not found
So it seems venv is not packaged with the Libreoffice embedded python environment. Is there a way to add it?
Second try : inject embedded environment inside a precreated venv
What I did here was to create the Venv in pycharm using a standard python version aligned with the one libreoffice uses (here 3.8.10). Once the venv is succesfully created, I manually change the path to the python based interpretor to the proper libreoffice one.
I also update the pythonpath in pycharm for this venv to include Files\Libreoffice\program\ so that we can find some preinstalled modules suitable for Libreoffice, in particular uno.py.
This looks a little better, I can launch a libreoffice instance, but when I actually want to do stuff inside this instance from python, I get the following error loading component library </mergedlo.dll> failed
This mergedlo.dll file is in a directory referenced in the python path for this venv.
What configuration did I miss here?

Pycharm is not showing Pygame properties [duplicate]

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

Pycharm storing packages in separate files?

I am using Python 3.7 on windows. I installed Pycharm and successfully wrote a script which I am now trying to schedule using Windows Task Scheduler, which comes with its on set of complications. One thing I have noticed about Pycharm is I think it has created a separate file directory to store any packages I add to a script (maybe in something called "venv"? Instead of using the User/Python37/Scripts file.
This means when I try to run my script in the command prompt, python.exe looks for packages and cannot find them. Also if I go into my Pycharm project folder is see another instance of a Python Application file different than the Python Application stored in User/Python37. I think this also creates problems but I am not 100% sure.
I am hoping someone has seen this issue and can help me align where Pycharm stores packages. Any help would be greatly appreciated.
You can also simply add your script/package into your python path.
For that follow this awser : How to add to the PYTHONPATH in Windows, so it finds my modules/packages?
PyCharm creates a virtual environment (venv) where you can keep the python version and the libraries used in a specific project.
You can add libraries to the specific environment through the Pycharm GUI:
File > Settings > Project: Patterns > Project Interpreter > Install (green +)
Find your package and click Install Package in your venv.
You can see all the installed packages and their version in the path:
File > Settings > Project: Patterns > Project Interpreter
You can also use pip install, if you want to go through CLI, but be sure to use the virtual environment's pip (located in project_folder/venv/Scripts).
If for some reason you want to use the python version outside the virtual environment, go to the following path in PyCharm:
File > Settings > Project: Patterns > Project Interpreter
In the Project interpeter dropdown menu, you should find other python's location; choose the one you prefear. If you don't see your standard python version (usually in C:\python\python.exe, or something similar), you can add it by clicking on the settings menu, and specify the path to the desired python version in Base interpreter:.
In this window, you can find other settings to configure the interpreter as you want.

VSCode Python package inspection failure

This's probably quite off-topic but maybe someone else share my experience... when I started coding KiCAD plugins and learning Python I decided to use VSCode (with Microsoft Python extension) because I already use it for a number of other things and works great.
I have two main issues mostly bound to Python extension itself other than VSCode:
VSCode try to use python 3.7 binary (I installed it on my
machine and I need it) instead kicad/python 2 (it's installed on a different location);
even if I setup VSCode to look for additional packages with a specific configuration file (settings.json):
"python.autoComplete.extraPaths": ["C:/Program Files/KiCad/lib/python2.7/site-packages"]
VSCode refuse to investigate pcbnew.py (located in that folder) and thus every Intellisense like function (inspection etc) doesn't work.
How can I fix that? ... I'm more interested in fixing the second issue other than the first.
To correctly enable per project configuration of VSCode / Python open folder and create a workspace configuration for the project itself (i.e. create a .vscode folder inside project folder and store a settings.json file inside it with you custom configuration). In my specific case I needed to specifiy a custom location for an older Python binary trough python.pythonPath directive.

Please suggest best practices for using Virtualenv in Pycharm on Windows

I’m starting to learn Python using PyCharm IDE on windows. I have a specific question about where to start adding/ creating python files/ classes.
I installed Python 3.5 and installed PyCharm Community. Created a project first and then created a virtualenv. I can therefore switch between 'Python Interpreters'. But, when I open PyCharm project, I see a folder structure and I can’t make a decision where to start creating code files. I've added screenshot as reference.
Where to start adding creating code file packages? And, is this the right way of creating virtualenv?
I'd also appreciate if you could please suggest any related best practices, especially regarding using virtualenv in this scenario.
Create the virtualenv while creating the project. Click the gear icon to the right of the interpreter box at the new project dialog box. Choose "Create VirtuanEnv"
Then you can right click the project name and add a python file.
Typically when developing python code on windows using PyCharm, I create a subfolder in my User directory (C:/Users/Vasili/virtualenvs/), where I place all of my future virtual environments.
When creating a new project, I create a new virtualenv in that folder and set it as the interpreter for the project. PyCharm will use that venv every time you open that project. It will also activate it whenever you use the inline terminal within PyCharm so you can run executables such as nosetests, if you have it installed in the project virtualenv.
As for general project structure, python is fairly informal when it comes to this, but there are some standards that you could try to adhere to, such as:
creating a setup.py file with your project metadata and other data to allow uploading the project to pypi, or even building RPMS.
a requirements.txt file with packages that pip must install for the project to work
A tests folder with your unit tests, fixtures and so on
The actual package itself, named after your project, with a dunder init script (__init__.py) inside, to signify that it is a package
An open source license
A readme, or documentation that can generate HTML docs with sphinx
Anything else you think would help people use your software.
As you are using community edition, you will have some limitations of the features.
I would suggest you to create virtual env from terminal/command prompt.
This is the folder structure which I follow for development while using Pycharm:
C:\workspace\<virtual_env>
C:\workspace\<project_name>
Add the whole workspace folder to your Pycharm as project.
In order to create and activate virtual env in windows refer the following:
https://docs.djangoproject.com/en/1.10/howto/windows/

Categories

Resources