VS Code intellisense appears to not be working - python

So I have this simple code below that I am trying to use and I wonder if I am expecting too much from intellisense in VS Code.
I installed the Trello python library using pip3 install py-trello.
Using VS Code (1.27.2), and Python extension (2018.8), Python (3.7.0), Mac OS (10.13.6).
In the code below, if I type board. then I do not see any intellisense help after typing the dot.
I guess it may not be clear to VS Code that board is a class type returned by client.list_boards().
from trello import TrelloClient
client = TrelloClient(
api_key='my_api',
token='my_token',
)
for board in client.list_boards():
print(board.name)
I do get Intellisense popup, if I do this.
x = input("Name: ")
x.
I get a dropdown after x.
However, I do not get intellisense popup with this below sample code either. After I type calc. there is no intellisense popup. Copied this code from another issue on Github but I am using Python 3.7.0. I am facing the same issue, although this issue seems to have been resolved and my application versions are much higher than when the issue was raised and resolved.
class FactorMixin:
Factor_1 = 1
class Calc:
def sum(self, a, b):
return a + b
class BetterCalc(Calc, FactorMixin):
def multiply_factor(self,a):
return a * self.Factor_1
var_a=1
calc = BetterCalc()
calc.
calc.m shows intellisense popup, but calc. does not
And finally, here is one time when the popup does show up. Not sure what is happening and whether it is the language server starting late as some people have experienced in the past...
And when I set "python.jediEnabled": true in settings, the intellisense does pop up, but I wonder what I am losing out on by not using the python server...
Thank you.

Related

How do I start a COM server? Code is in Python

I want to run Python code as a COM server. Eventually I want to run an RTD server available here. But first I want to know what exactly you have to do to getting any COM server running. So let's focus on this example.
class HelloWorld:
_reg_clsid_ = "{7CC9F362-486D-11D1-BB48-0000E838A65F}"
_reg_desc_ = "Python Test COM Server"
_reg_progid_ = "Python.TestServer"
_public_methods_ = ['Hello']
_public_attrs_ = ['softspace', 'noCalls']
_readonly_attrs_ = ['noCalls']
def __init__(self):
self.softspace = 1
self.noCalls = 0
def Hello(self, who):
self.noCalls = self.noCalls + 1
# insert "softspace" number of spaces
return "Hello" + " " * self.softspace + who
if __name__=='__main__':
import win32com.server.register
win32com.server.register.UseCommandLine(HelloWorld)
Ok, this works in the way that there were no errors and server is registered, hence it is available in the HKEY_CLASSES_ROOT registry. But what can I do with this? Some say you have to compile a instance and have a .dll or .exe file. WHat else do I have to do?
Well, I ran your example. The registry key for the server is at:
HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{7CC9F362-486D-11D1-BB48-0000E838A65F}
It has two subkeys... one for LocalServer32 and one for InProcServer32
I created a simple VBA macro in Excel:
Sub d()
Set obj = CreateObject("Python.TestServer")
MsgBox obj.Hello("joe")
End Sub
Macro ran just fine. My version of Excel is 64-bit. I ran the macro and then fired up Task Manager while the message box was being displayed. I could see pythonw.exe running in the background.
The only difference between my python script and yours is probably the name and also that I added a line to print to make sure I was executing the function:
if __name__=='__main__':
import win32com.server.register
print("Going to register...")
win32com.server.register.UseCommandLine(HelloWorld)
When I ran the 64-bit csript.exe test, it worked... as expected... when I ran the 32-bit version it failed.
I know why...sort of...
The registry entry for InProcServer32 is pythoncom36.dll
That's no good. It is an incomplete path. I tried modifying the path variable on my shell to add to one of the 3 places where the DLL existed on my system, but it didn't work. Also, tried coding the path in the InProcServer32. That didn't work.. kept saying it couldn't find the file.
I ran procmon, and then I observerved that it couldn't load vcruntime140.dll. Found the directory under python where those files were, and added to my path. It got further along. If I cared enough, I might try more. Eventually using procmon, I could find all the problems. But you can do that.
My simple solution was to rename the key InProcServer32 for the CLSID to be _InProcServer32. How does that work? Well, the system can't find InProcServer32 so it always uses LocalServer32--for 32-bit and 64-bit processes. If you need the speed of in process then you'd need to fix the problem by using procmon and being relentless until you solved all the File Not Found errors and such. But, if you don't need the speed of in process, then just using the LocalServer32 might solve the problem.
Caveats I'm using an Anaconda distro that my employer limits access to and I can only install it from the employee store. YMMV.

Name 'Actor' is not defined

I have a problem with python programming, when I'm trying to write a game (introduced by the book: Coding Games Python DK 3), it says:
name 'Actor' is not defined.
here's my code:
import pgzrun
from random import randint
WIDTH = 400
HEIGHT = 400
dots = []
lines = []
next_dot = 0
for dot in range(0, 10):
actor = Actor("dot")
actor.pos = randint(20, WIDTH -20), randint (20, HEIGHT - 20)
dots.append(actor)
def draw():
screen.fill("black")
number = 1
for dot in dots:
screen.draw.text(str(number), (dot.pos[0], dot.pos[1] + 12))
dot.draw()
number = number + 1
for line in lines:
screen.draw.line(line[0], line[1], (100, 0, 0))
pgzrun.go()
You are using the Python library pgzero (indirectly via importing pgzrun).
I had refactored my game code into multiple files (imported into the main file) and did also observe the same strange
NameError: name 'Actor' is not defined
error message.
The Actor class seems to be "private", but can be imported with this simple code line:
from pgzero.builtins import Actor, animate, keyboard
For background see:
https://github.com/lordmauve/pgzero/issues/61
Update Aug 18, 2019: The screen object cannot be imported since it is created as global variable during runtime (object = instance of the Screen class) and IDE-supported code completion is not possible then. See the source code: https://github.com/lordmauve/pgzero/blob/master/pgzero/game.py (esp. the def reinit_screen part)
This page on the Pygame site will help you run it from your IDE: https://pygame-zero.readthedocs.io/en/stable/ide-mode.html
Essentially, you have to have these two lines of code:
import pgzrun
...
...
pgzrun.go()
But during coding, the IDE will still complain that objects and functions like screen and Actor are undefined. Pretty annoying. As far as I know, there's no way to fix it, you just have to ignore the complaints and hit Debug > Run. Provided you have no mistakes, the program will compile and run.
With regards to
import pgzrun
actor = pgzrun.Actor("dot")
Or
from pgzrun import *
dot=Actor("dot")
Neither of these help integrated development environments like Spyder or Visual Studio recognise objects and functions like screen and Actor
pgzrun doesn't seem to behave like a normal python library.
Pygame relies on using pgzrun to run your python script from the command line:
pgzrun mygame.py
In chapter 5, page 73 of Coding Games in Python, step 2 is to save the file as numbers.py. This creates a conflict with the built in module numbers. This is the cause of the error "name 'Actor' is not defined". The solution is to save the file with a different name (i.e. follow.py).
Please define the class Actor or import it from package if you have it in your pip packages or in same dir
From what I could find on the Pygame Documentation website, Actor is defined in the pgzrun package. With your current import statements, you would have to call the Actor constructor by
actor = pgzrun.Actor("dot")
which would show the compiler that Actor belongs to pgzrun.
Alternatively, if you wanted to just use Actor("dot"), you could change your import statement to
from pgzrun import *
which means "import everything from pgzrun". This also keeps track of what comes from pgzrun and tells the compiler, but it could lead to issues (eg. if you define your own Actor constructor in your code, the compiler wouldn't know which one you're trying to use).
well I just found out It has no problem running with cmd, it has problem with running from the software itself.
So, code is correct but I might suspect something. I think you didnt upload a picture called "dot" in pgzero.
I was getting the same error message, until finally I found this topic here. The book version is missing those pgzrun first and last lines.
If you're still getting the error after putting those lines in, I bet your 'python' points to python2. Try this at the command-line:
python -V
If it shows a version of 2, you'll need to fix that. Check
which python
If it's in /usr/bin, you can do:
sudo su
cd /usr/bin
rm python
ln -s python3 python
If it's in an update-alternatives directory, you can run that program to fix it.
I had the exact same problem at the exact same code. After weeks of deleting and rewriting everything that has to do with python on my computer, I realised that it runs if you make the screen. ... lines comments by writing # in the start of each of them. I haven't found how to fix it yet, but I will inform when I will.
I found the issue. Everything on my computer was uploaded automatically to OneDrive. As a result I had a file with the same name on my computer and on OneDrive and when I tried to run it, it always ran from OneDrive where pygame and pgzero are not installed. I solved it by disconnecting auto upload and by changing the name of the file that was on my computer. Hope this works!
the solution is very simple. do not give the file a name.py name because it conflicts with python numbers.py

Writing a plugin to open a new file

In Sublime Text 3 I have a few packages that open new files within the window. They have functions that look like:
class BufferCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.new_file()
...
I grabbed this for something I am working on, and couldn't get a new file to generate, even though that works for the plugin I snagged it from, and is similar to how it is used in Packages/Default/new_templates.py. After some more searching, I found the following code, and it works as expected
class TestCommand(sublime_plugin.TextCommand):
def run(self,edit):
view = self.view.window().new_file()
Can somebody tell me why?
It might be the way you are running your command? If you are entering
view.run_command("buffer")
into the python console, then nothing will happen if BufferCommand is not a TextCommand.
Instead try this:
window.run_command("buffer")
This should fix your problem.

python rounds automatically when i work trhough spyder, but not when i type in direcly in console =S

so i am making a class of employess, and i want to make a function that returns total time worked
try
1, running this through spyder
2. running it in console
gives different results :S
class Employee(object):
def __init__(self, wage, wage1, hours_accounting=[]):
self.wage=wage
self.wage1=wage1
self.hours_accounting=hours_accounting
def work(self):
A=H,M=input('when did you start work today? format hh,mm')
B=H1,M1=input('when did you stop work today? format hh,mm')
total_time_in_hours=H1-H + (M1-M)/60
return total_time_in_hours
amanda=Employee(100, 50)
amanda.work()
07,15
21,00
#
why?? thanks in advance!
(Spyder dev here) Before Spyder 2.3.0 we ran this code while starting a console:
from __future__ import division
which makes things like 2/3 to return 0.6666 instead of 0 (which is the normal behavior in Python 2).
To change this you have three options:
Update to 2.3.0
If you are using a Python Console, you need to go to
Preferences > Console > Advanced Settings > PYTHONSTARTUP replacement
and select the option called
Default PYTHONSTARTUP script.
If you are using the IPython console, you need to go to
Preferences > IPython Console > Graphics
and deselect the option called
Automatically load Pylab and NumPy modules

How do you listen to notifications from iTunes on a Mac (Using the NSDistributedNotificationCenter)

Looking for help/tutorials/sample code of using python to listen to distributed notifications from applications on a mac. I know the py-objc lib is the bridge between python and mac/cocoa classes, and the Foundation library can be used to add observers, but looking for examples or tutorials on how to use this to monitor iTunes.
If anyone comes by to this question, i figured out how to listen, the code below works. However accessing attributes do not seem to work like standard python attribute access.
Update: you do not access attributes as you would in python i.e (.x), the code has been updated below, it now generates a dict called song_details.
Update3: Update to the code, now subclassing NSObject, removed adding the addObserver from the class. Will keep the code updated on github, no more updates here.
import Foundation
from AppKit import *
from PyObjCTools import AppHelper
class GetSongs(NSObject):
def getMySongs_(self, song):
song_details = {}
ui = song.userInfo()
for x in ui:
song_details[x] = ui.objectForKey_(x)
print song_details
nc = Foundation.NSDistributedNotificationCenter.defaultCenter()
GetSongs = GetSongs.new()
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.apple.iTunes.playerInfo',None)
NSLog("Listening for new tunes....")
AppHelper.runConsoleEventLoop()
The source code for GrowlTunes might give you some clues here. You'd have to translate from Objective-C to PyObjC, but eh, whatever. :)
GrowlTurnesController.m
(Or grab the whole growl source tree and navigate to GrowlTunes so you can see it all in action.: here's a link to the directions on how to get the source

Categories

Resources