What should I do to Pycharm code suggestion? - python

enter image description here
I'm a newbie in Python. I use Pychram to code. When I type 'pri' in Win 10, Pycharm suggests 'print'. But in Ubuntu 20, Pycharm doesn't do like that. I already set in Code Completion but it does not work.What can I do?
My English is bad, sorry for that.

#Gareth_Latty says the following in another similar post.
"
As Python is a dynamically typed language, you need to ensure it can work out what type things are, and inspect on the libraries on your system correctly. Try to make sure it's obvious what type the object is in your code.
One good way as of PyCharm 2.7 (back when versions were numbers) is to enable runtime type detection - PyCharm hooks into your program while it runs (while debugging), and checks the types of variables as they are used.
You can enable this by going to settings, going to the "Build, Execution, Deployment" section and then the "Python Debugger" subsection and enabling "Collect run-time types information for code insight"."
Please make sure to research and look at other posts. Thank You!
Make sure you check out this website and This one

Related

Python note book autocomplete not working in VSCode with environment being CVE

I have done some research, but none of the solutions work for me and it seems that my situation is much worse than everybody else. Currently, my VSCode editor is just acting like a plain text editor for any ipynb files. For example, this is how my import block looks like:
Nothing has been highlighted, and I'm working with this for quite a long time and now I can't bear with this anymore. If this is not bad enough, then take a look at the following:
Literally, nothing happens. I think now the only help I get from VSCode is plain text auto-completion, but that is far from what anyone should have.
The configuration is pretty simple. I have the following kernels I can choose from:
And I'm mainly using anaconda3 for my kernel. Further, I only have
in my setting.json for python configuration.
As for extension, I have the standard python extension with python extension pack installed, which are the only two extensions I installed for python.
I'm really tired of working with plain text python code, if any information is needed, I can provide as much as I can.
-- Edit
It seems that the default language is not python, but CVE. Futhermore, I can't find the proper language to choose. All the language options are listed below.
The error happened because of the extension Dependency Analytics. Please uninstall it and reload window.
Reference: jupyter conflict with Dependency Analytics.
Click here and select Python.
CVE seems to be the default language for notebooks for some reason. Please check the settings.

PyCharm autocomplete fails so show all options

I have recently gotten into python and began using PyCharm, mainly for the code completion feature, as it helps me familiarize with new packages and libraries.
While working with discord.py, I have discovered that the autocompletion feature does not show all possible options.
To show an example, this code is valid and can be executed:
However, if I attempt to use autocomplete to fill out line 11, neither user or name is offered.
Example 2
Example 3
Oddly enough, the autocomplete does work for a large portion of the package though, as shown here:
I have struggled for a long time trying to figure out why this happens. I have made sure I'm using the correct interpreter (discord.py is shown in the installed packages) and I have enabled "Collect run-time types information for code insight" under Python Debugger in Settings, as I have read that this might help.
I'll be thankful for any insight into this.
discord.Client uses __getattr__ to handle attribute lookup for the user attribute dynamically. __getattr__ is fundamentally incompatible with the kind of static analysis PyCharm uses for autocompletion.
I'd probably just live with PyCharm not being able to find this attribute. If you want to do something about it anyway, writing type stubs might help (or they might go out of sync with the implementation and cause more problems), or you could see if discord.py would accept a pull request to use properties instead of __getattr__/__setattr__ for the attribute forwarding they're doing here.

Does there a tool exists which can help programmer avoid Python NameError?

I am not a native English speaker. When I code with Python, I often make spelling mistakes and get 'NameError' Exceptions. Unit test can solve some problems but not all. Because one can hardly construct test cases which cover all logic. So I think a tool that detect such errors would help me a lot but I searched Google and cannot find it.
I believe that testing your code via a static analyzer (e.g. pylint) will help a lot.
Another hint: you can use a fancy IDE with smart auto-completion which will reduce the amount of such mistakes.
I prefer eclipse with pydev integration for python development projects.
It will solve your purpose and will show you the errors in RED before you run your program. but it requires your project to be properly configured under eclipse as below:
Configure Python interpreter after integrating PyDev with eclipse
Create / Import your project & set as python pydev project
Configure your source folder ( Give information to eclipse that which folders contains python source code under your entire project tree)
You're done & setup now for general python programming setup.
Further more you can integrate your Eclipse/Pydev project with pylint as mentioned by BasicWolf which checks your code quality & bugs on the go while you're coding.
References:
Installing PyDev under Eclise
Eclipse PyDev Integration
Troubleshooting PyDev/PyLint Integration
pylint can help here. It will report those locations as
E: 2,6: Undefined variable "foo"
for example.
You could get an IDE which helps a bit with autocompletion of names, though not in all situations. PyDev is one such IDE with autocompletion; PyCharm is another (not free).
Using autocomplete is probably your best bet to solve your problem in the long term. Even if you find a tool which attempts to correct such spelling errors, that will not solve the initial problem and will probably just cause new ones.

Debugging Python code in Notepad++

I use Notepad++ for writing and running Python scripts. It is a great text editor, except for debugging. Is there a way to step through the code, use break points, view variable values etc. in Notepad++ like you can in Visual Studio?
Does such a plug-in exist? Not that I know of. I agree completely with qor72 on that note.
Is it possible to create such a plugin / functionality? Possibly.
After doing some quick digging, I did find a plugin that looks promising, Python Script. In short it allows you to run python scripts that can access the NPP modules (file menus etc...) as well as the Scintilla Methods which appear to give access to things like the markers on the pages.
To accomplish such a feat I could see the task being broken into a few large blocks (I feel a new open-source project coming on...)
Using Python Script, integrate the python debugger(PDB) as mentioned by Shashi.
Using the Scintilla Methods, add trace back calls where a NPP marker is placed
Redirect PDB outputs and process them to show where the file is stopped (again using the Scintilla methods).
While at the newly created breakpoint and using PDB determine all of the variables in the current namespace. Take this info and dump it to a CMD window, or if you want to get fancy some GUI created with Tk / wxPython
Closing Thoughts
While I think it's possible to create such a plug in, it would be quite an undertaking. Along that line, you might be better off trying to find a different editor that has this built into it already and just create macros (or whatever the IDE calls them) to add in the things you like most about NPP.
Please note that I am a daily user of NPP and have been for many years so I definitely understand why you'd like to have the functionally added to NPP. One of my favorite things about NPP is the speed in which it opens and searches files... moving to a bloated IDE, IMO, would not be worth it to me.
My current work flow is to do all of my editing in NPP and just double click to run the modules. If it fails or goes off in the weeds, I launch IDLE to debug it.
I really hope someone tells me I'm wrong (I'd love to have that feature in Notepad++) but, Notepad++ is designed as a programmers editor, not an IDE. While it has a lot of cool functionality, that level of debugging isn't part of the core tool.
Not seeing anything in the npp-plugins either.
I think python debugger
is the best option if editor is not providing facility :)
Quick guide:
from pdb import set_trace as bp
code
code
bp()
code
code
At the (Pdb) prompt, enter s to step, p foo to print foo, and c to continue executing the code until hitting another breakpoint.
Have you thought of using Komodo.
It's open source and has ports for Windows, Linux and MAC (I think).
This may be an alternative, and if you want some advice from notepad++ users, have a look at the following post on this very site:
Komodo Edit and Notepad++ ::: Pros & Cons ::: Python dev
Some npp users here seemed to have made the switch for python editing running etc...
personally don't know much about debugging on Komodo but as it's an IDE so would be surprised if you couldn't do it easily
I don't really see why Shashi's answer hasn't been upvoted. For the link that he has given supplies a way to step through python scripts as the OP has requested.
So for all who don't know about the pdb module, upon importing it the pdb.set_trace() function allows one to step through the area of code after it. And it is very much similar to the visual studios method of debugging. While you're stepping through the code you are able to input a variety of commands.
One of them is p <expression> and that allows the user to print the current state of variables within the local and global scope.
I know it's 11 years on, and I'm a bit late to the game, and I know it's not Notepad++ but please do consider Visual Studio Code.
It's free, easy to install (both the editor itself plus any python interpreters it uses) and it's widely used and nowhere near as bloated as it's Visual Studio counterpart. It also appears to be the IDE of choice for a lot of Cisco-related course material.
Write your code, click to the left of code pane to insert your breakpoints click the Debugger icon (highlighted), and you're away:

Autocompletion in dynamic language IDEs, specifically Python in PyDev

I'm new to Python, with a background in statically typed languages including lots and lots of Java.
I decided on PyDev in eclipse as an IDE after checking features/popularity etc.
I was stunned that auto-complete doesn't seem to work properly for builtins. For example if I try automcomplete on datafile after:
datafile = open(directory+"/"+account, 'r')
datafile.
No useful methods are suggested (e.g. realines). Only things like call.
I am used to learning a language by jumping into class definitions and using lots of auto-complete to quickly view what a class will do. My PyDev 'interpreter' is set up fine with 'forced builtins'.
Is it possible to get auto-complete for builtins with PyDev? Am I approaching the IDE wrong, i.e. should have an interpreter running on the side and test stuff with it? So far the IDEs have seemed weak, e.g. IDLE segfaulted on my new mac after 2 minutes. I'd love to know what experienced Python developers do when exploring unfamiliar (builtin) modules, as this is making me reconsider my initial attraction to Python. I like a language you can learn by easy exploration!
Thanks,
In my opinion, the Python shell is a much better place to explore new modules than relying on an IDE. Don't forget, in Python you can do anything in the shell that you can do in a program, because there's no separate compilation step. And in the shell, you can use dir(x) to find all the properties and methods of x, whether x is a module, a class, or whatever.
Even better, the enhanced iPython shell does provide tab completion for all objects.
In fact because of this, many Python programmers - myself included - don't use an IDE at all, but just a simple text editor (I use VIM).
Just to keep it up to date so that new readers are not confused about the current state of Pydev - the example you gave now works in Pydev. (btw, one should avoid operating on paths manualy - use os.path.join instead)
I'd love to know what experienced
Python developers do when exploring
unfamiliar (builtin) modules
I use ipython. Ipython is an enhanced version of the interactive shell that adds tab completion and quick access to an object's doctstring. It also gives lots of other features that the standard shell does not have - you can find a summary of its features here.
Someone more knowledgeable here can give you a detailed answer. Here is a short one.
Autocomplete for a dynamically typed language can by nature never be as rich as that for a statically typed language. In the case of open for instance there is no way to figure out what will be the return type at the time of writing the code. The method signature does not include a return type unlike a statically typed language like Java. Consequently the IDE is not able to give you any hints.
You certainly should have an REPL running during any Python development. One advantage of an interpreted language is that you can test small chunks of your code on the REPL as you go along. It is also a good place to test your understanding of how built-ins and other modules work.
I work on Ubuntu so I do not know how easy or difficult it is to get IDLE running on a Mac. I usually work with the very handy iPython for REPL needs and use Pydev for other development (such as Django). You might want to give iPython a try.
You want IPython. As Daniel pointed out above, the interactive shell is a much better way to explore Python (and indeed, most other languages too).
This might help with setting it up on OSX.
You might want to take a look at WingIDE. It autocompletes your datafile correctly.
If it is unable to infer the type, you can use an assert like
assert isinstance(datafile, file)
to help the autocompleter out
I use PyDev at work so I know where you're coming from. If you're willing to consider other tools, have a look at JetBrains' PyCharm, that's my new preferred Python IDE for my own projects. No affiliation to speak of except to say I'll be picking it up when it's out of beta. :)

Categories