VSCode Python package inspection failure - python

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.

Related

Python Interpreter not showing anything in PyCharm

Issue
So, I've used PyCharm for quite a while and I know how to set environments, etc. For some reason, I can't seem to access the Python interpreters that I've got (see image).
When I open the same project in Clion I am able to run the files and I can see the interpreters.
Attempted solutions
Run in Clion to see if the interpreter "sticks".
Removed the .idea file.
Reinstalled PyCharm.
Usually Python Interpreter is a sub-item of Build, Execution, Deployment in the Settings. (In PyCharm 2021, the layout in PyCharm 2019 was slightly different).
The fact the item appears separately is a strong hint that you either installed a plugin or for some reason the IDE was opened in LigthEdit mode (this can easily happen if you launch the IDE using the command-line). This can also happen if you don't have an open project (having files open is not the same as opening a project, as such, with an .idea folder - the IDE configuration files will be different.)
Having the IDE in LightEdit mode provides only a limited set of functionalities, so the IDE doesn't allow you to configure an Interpreter in the usual way (other setting dialogues will also behave different from usual).
The solution is to Exit the LightEdit mode, opening your previous project or a new one by going to File > Open or File > New Project.

Can I just create files and "work as normal" under a python folder with a virtual environment on it via vscode?

I wasn't sure exactly how to ask my question in one concise sentence for the title so please give me the chance to be a bit more specific here.
So I've recently learned and have been told that working in virtual environments, or getting in the habit of doing so, is best for python (and I assume for many other applicable languages). Thus, I can create a virtual environment via virtualenv on the mac console and get it to activate and deactivate all that shenanigans in console. For example, when I work with this folder I have created a virtual environment for (via console) in vscode, it looks like this. I made sure that the python it was running seen on the bottom left of the screenshot included the virtual environment python noted by the "('learnpp')" symbol as I assume it's what I want. Now, my question(s) are:
do I have to go activate/deactivate the virtual environment on console before I start to work on anything despite having it set up like so (like in the screenshot) for the environment to actually work every time or does the IDE take care of it?
in the same screenshot, everything except the "draft.py" file was preloaded as a result of creating the virtual environment in seen folder which I strongly assume manages/runs the virtual environment so I was wondering if it was ok to "go about my day" creating files and folders despite all the mentioned things present. (I imagine I would just create a separate folder for the application I am creating to make life easier within the virtual environment folder OR would I be better off making an entirely new folder from the root folder?) In other words, instead of LEARN.PP/learnpp/"folder for my application" I would just create a new folder from LEARN.PP/ separately.
anything else useful I should know? (feel free to throw anything at me since I am a beginner)
Lastly, thanks for your time and I hope you have a great day!
For the virtual environment in VS Code, it is recommended that you separate its folders and scripts to avoid the impact of file interference, and separating them is easier to manage.
For example: 1. ".env" and ".env_3.9" are two virtual environment folders, which have their own dependencies and installed modules and so on; 2. ".vscode" is the folder where the configuration files are stored (settings.json; launch.json; tasks.json and so on.); 3. "a_De" is the folder where the python script is stored. and we can also store other types of files (executable files, read files and so on).
For more information about the use of python in VS Code, you can refer to: Editing Python in Visual Studio Code and Using Python environments in VS Code and Integrated Terminal.

Project Functionality in Python similar to RStudio

I am trying to learn Python coming from R. I am used to the Project functionality provided by RStudio, that I can create a new Project and start a new session loading the file with '.Rproj'. The new session automatically uses the folder as the working directory and then I can set all the paths relatively. No need to change anything for different computers etc.
I tried asking people more familiar with python but usually nobody really understood my question. I heard, this would not be a problem once i started using jupyter or other stuff correctly when I were to use my files on another session.
My questions is then either if there is something like the Project functionality from RStudio or how do I better manage my projects across different computer. I could not any good links on that.
I am not so much familiar with R studio, but the functionality that you described are available on spyder IDE. You have dedicated project window and you and switch between project. There are many more functionalities (lie an always-on python console, variable/function display).
Please check
https://www.spyder-ide.org/
You can further use the python os package to make your code os independent.
I have the same issue. Though the main thing I use RStudio Projects for is to manage the working directory, and open RStudio in the right place. The best I've come up with is very specific to my problem - it doesn't do any of the other stuff .rproj and RStudio do, and isn't terribly portable.
I'm on Windows, using anaconda and JupyterLab in (it's not quite RStudio, but seems a good option to me) and I've found it a pain to navigate to the right location with the right environment for each project.
My understanding of python isn't great, but it looks like with jupyter lab .ipynb files the default working directory is the directory the file is in (so doesn't suffer from the chronic setwd() issue that .rproj helps to eliminate).
So I've created a .bat file (called pyproj.bat in this case) in the root of my project folder structure, to open jupyterlab in the right python environment:
call <anaconda_dir>/Scripts/activate.bat
call conda activate <project_environment_name>
call jupyter lab
cmd /k
My first line of that script is call %USERPROFILE%/Anaconda3/Scripts/activate.bat which at least gives it a chance of being portable between users in my organisation.
Would be nice if there were something a bit more automatic that could handle this - e.g. better searching for the activate script, and perhaps the environment management too (perhaps write the .yml and offer to build the environment if it's not present)
(Borrowed some the .bat idea from this question: How to make batch files run in anaconda prompt)

How to import ros to PyCharm

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!

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