VS Code Autocomplete python - python

I'm pretty new to python and would like to have autocompletion using vs code inside my project.
For external modules it works fine but I dont get autocompletion for my own files/modules.
For example, I have a main.py file inside my src folder and a roboter.py next to it.
I import a Roboter class via from roboter import Roboter and it runs fine but I dont get any autocompletion on it.
Also, I'm using no virtual env but have all other packages global on which I do get autocompletion.
What am I missing here?

Okay, installing a 64 bit version of python instead of a 32 bit version did the job.

Related

VS Code / Pylance / Pylint Cannot resolve import

The Summary
I have a python import that works when run from the VS Code terminal, but that VS Code's editor is giving warnings about. Also, "Go to Definition" doesn't work.
The Problem
I have created a docker container from the image tensorflow/tensorflow:1.15.2-py3, then attach to it using VS Code's "Remote- Containers" extension. Then I've created the following file in the container.
main.py:
import tensorflow.compat.v1 as tf
print(tf.__version__)
This runs fine in the VS Code terminal, but the Editor and the Problems pane both give me an unresolved import 'tensorflow.compat' warning. Also "Go to Definition" doesn't work on tf.__version__.
I'm using several extensions but I believe the relevant ones are the Microsoft Python extension (installed in the container), as well as the Remote - Containers extension, and now the Pylance extension (installed in the container).
The Things I've Tried
I've tried this with the default pylint, and then also after installing pylance with similar results. I've also seen some docs about similar issues, but they were related to setting the correct source folder location for modules that were part of a project. In contrast, my code within my project seems to work fine with imports/go-to-definition. It's external libraries that don't seem to work.
Also, for the sake of this minimal example, I've attached to the container as root, so I am guessing it's not an issue of elevated permissions.
I've also tried disabling all the extensions except the following, but got the same results:
Remote - Containers (local)
Remote - WSL (local)
Python (on container)
Jupyter (on container, required by Python for some reason)
All the extensions above are on the latest versions.
I've also fiddled around with setting python.autocomplete.extraPaths, but I'm not sure what the right path is. It also seems like the wrong thing to have to add libraries to the path that are installed in the global python installation, especially since I'm not using a virtual environment (it being in a docker container and all).
The Question
How do I fix VS Code so that it recognizes this import and I can use "Go to Definition" to explore these tensorflow functions/classes/etc?
tldr;
TensorFlow defines some of its modules in a way that pylint & pylance aren't able to recognize. These errors don't necessarily indicate an incorrect setup.
To Fix:
pylint: The pylint warnings are safely ignored.
Intellisense: The best way I know of at the moment to fix Intellisense is to replace the imports with the modules they are aliasing (found by importing alias in a repl as x then running help(x)). Because the target of the alias in my case is an internal name, you probably don't want to check in these changes to source control. Not ideal.
Details
Regarding the linting: It seems that tensorflow defines its modules in a way that the tools can't understand. Also, it appears that the package is an alias of some kind to another package. For example:
import tensorflow.compat.v1 as tf
tf.estimator.RunConfig()
The above code gives the pylint warning and breaks intellisense. But if you manually import the above in a REPL and run help(tf), it shows you the below package, which you can use instead:
import tensorflow_core._api.v1.compat.v1 as tf
tf.estimator.RunConfig()
This second example does not cause the pylint warning. Also the Intellisense features (Go to definition, Ctrl+Click, etc) work with this second example.
However, based on the _api, it looks like that second package name is an internal namespace, so I'm guessing it is probably best to only use this internal name for local debugging.
Confirmation/Tickets
pylint: I've found a ticket about pylint having issues with a couple tensorflow imports that looks related.
Intellisense: I've opened a ticket with pylance.
So for me I was trying to
import pandas as pd
but I got the error
"pd" is not accessedPylance (module) pd
SO what I did was reload the extension Python IntelliSense(Pylance) and that solved my issue.
I had the same problem but with all kinds of packages.
My solution was to go to the VSCode settings and search for "python.analysis.extraPaths", and add the path to your site-packages.
In my case, I added C:\Code\Python39\Lib\site-packages, and now it's working fine.
What, usually, solves the pylance issues for me is pointing my Python interpreter to the virtualenv one.
Open the command palette Ctrl + Shift + P
Type: Python: Select Interpreter
It will show a list of all the python Interpreters it actually detects:
Select Enter interpreter path
Type in the path to your local venv/bin folder or click find to navigate using the file explorer.
Your path should look something like:
venv/bin/python3.9
i changed "import tensorflow as tf" to "from tensorflow import compat as tf"
it ll even work for tf.gfile.Gfile()

Importing a module works in IDE but doesn't work in windows command line (edited)

I am new to Python so this may be a stupid question, sorry in advance if it is so!
The paths Python searches for modules using Spyder 4.1.4.:
[.., 'D:\Users\Martin\Anaconda3\lib\site-packages', ..]
The paths Python searches for when used via command line in windows:
[.., 'D:\Users\Martin\Anaconda3\lib\site-packages', ..]
Thus, both include path to
'D:\Users\Martin\Anaconda3\lib\site-packages'
where I store my cv2 module installed. Yet, Spyder can import cv2 and to my surprise cmd cannot import cv2 with an error No module named 'cv2'. What am I missing please? Can anyone verify if it works for him in command line please? (I changed/edited the question again to cut unneccessary long talk, though the essence stayed the same, in hope someone could answer).
I solved it. If you have a similar problem, you can check if your case is similar to mine and follow the steps.
I use Spyder and everything works fine. However my goal is to use Python via cmd. This could not be done because Python via cmd could not find my modules.
What I did: After typing python in cmd, Python worked and output its version by default. However, I noticed the version was different than version running in Spyder - I had two Python installations (one of which I didn't know about). I found the installation cmd uses by import sys and print(sys.executable). I uninstalled that Python installation.
Now, typing python in cmd does not do anything, it is an unknown command. Then I followed this guide to add my Python installation to Path (using Windows) by:
My Computer > Properties > Advanced System Settings > Environment Variables >
in System variables, I found "Path", clicked Edit, New, then input my Python installation path D:\Users\Martin\Anaconda3 as well as D:\Users\Martin\Anaconda3\Scripts and D:\Users\Martin\Anaconda3\Library\bin. This solved it for me.

How to properly import libtcod in PyCharm?

I'm trying to set up a roguelike Python project, but I can't seem to be able to import libtcod module into my project. This helloworld crashes, and the IDE keeps telling me that there is no module named libtcodpy.
import libtcodpy
def main():
print('Hello World!')
if __name__ == '__main__':
main()
What is the proper way to import modules into Python projects? I'm used to Java, so I was expecting something along the lines of Maven to manage the dependencies. There indeed seems to be something like that in PyCharm as well, this package manager for the venv, which from what I gather serves to isolate the project-specific stuff from the OS- or python-global stuff:
but libtcod simply isn't present in the rather exhaustive list of modules that appears after clicking on the "+" button, just some other module that has something to do with libtcod library (I guess?). Moreover, all the tutorials I found on setting libtcod up advise one to manually copy over files somewhere or run some command that I suppose does the importing somehow and other such solutions, all of which i tried and none of which worked. I don't want to pollute my project structure by using such hodgepodge ways of handling dependencies if I can at all avoid it.
Q: How do I get libtcod to work in my PyCharm project in the most clean and convention-abiding way possible?
Take a look at this github project called tcod: https://github.com/libtcod/python-tcod/blob/master/README.rst#installation
It's a python port of libtcod.
To install using pip, use the following command:
python -m pip install tcod
If you get the error "ImportError: DLL load failed: The specified module could not be found." when trying to import tcod/tdl then you may need the latest Microsoft Visual C runtime.
Blockquote

Where to put the unreal engine python library for pylint

I'm playing around with the Unreal Engine 4 and the python plugin. Adding it and getting it to work is no problem (win64, python 3.6.5 64bit) but since almost every py file for unreal starts out with import unreal_engine and I don't have that installed to the system like I would with something from pip install I'm wondering where to put what part of that repo so I don't have the pylint error [pylint] E0401:Unable to import 'unreal_engine' and lots of other "errors" like the following:
class Hero:
# this is called on game start
def begin_play(self):
self.startZ = self.uobject.get_actor_location().z
the last line with self. presents a linter error of [pylint] E1101:Instance of 'Hero' has no 'uobject' member.
Again I want to stress that all the code works inside of Unreal but it would be really nice to have the IDE pick the classes up right so I also get the autocomplete suggestions for available methods on uobject.something.
I'm fairly new to python programming and only ever had to import system libraries or things installed via pip so this is confusing to me.
For reference I have installed python via the official python installer for Windows to c:\dev\Python\Python36. The bin folder is in windows PATH and all that fun stuff.

Cannot import Twython

I installed Twython 1.2 using the Windows installer at this link: https://pypi.python.org/pypi/twython/1.2. The installer seems to run fine.
I get the error "ImportError: cannot import name Twython" when I try to do:
from twython import Twython
from twython import TwythonStreamer
Does anybody know why I cannot import twython?
Mmmm, Twython's current version is 3.x.x, not 1.2. I think that 1.2 installer is from yeaaars ago when I first started the project - the Twython API and structure has changed a ton since then.
Usually you need to set the environment variable PYTHONPATH to point to the path containing the file twython.py. I'm not sure exactly where Twython installs itself, but presumably it told you during the installation. If not, search for the file twython.py, and the directory it's in is the one you want. I'm not sure how you're running your Python script that imports Twython. If it's from the DOS prompt, try something like
set PYTHONPATH="c:\Program Files\Twython1.2\SOMEDIR"
substituting the actual directory containing twython.py (which might not be in c:\Program Files).
If you're running your script graphically, then if you're running inside some sort of Python IDE, it might have a way of setting PYTHONPATH in its Preferences. Otherwise, you need to go to the Control Panel and find the place where you can set environment variables. It's been awhile since I've used Windows and things have changed recently but it might be under Startup or similar, then under Advanced... or similar.

Categories

Resources