Vs code doesn't regognize tkinter on pop os - python

I have a code, where i use tkinter to make a window and stuff. It's a brawler picker for brawl stars. Im using pop os-linux and vs code and i have tried so many ways, but anything doesn't work.
When i run the code, i get this:
(.venv) sh-5.1$ python -u "/home/"my_name"/Documents/Vs-code_projektit/Joku.py"
Traceback (most recent call last):
File "/home/"my_name"/Documents/Vs-code_projektit/Joku.py", line 2, in <module>
from tkinter import *
ModuleNotFoundError: No module named 'tkinter'
And in the vs code itself, it regognizes the tkinter and turns the text green, but after that nothing relating to tkinter doesn't work and i shows an error. Btw i have the full code already cause i copied from my dual boot windows and i wanted to try it here,
What should i do to make it work?
EDIT:
For everybody who has the same problem, it may be caused by the app version. I posted this on reddit cause i didn't get answer in time, and someone suggested that i download vs code with appimage, snap, or in my case pop!_os Installation.
MAIN POINT: Someone said that NEVER use FLATPACK with ides. It may work for other apps but never use it with ides. It can't handle system packages or modules.

First of all, you need to know what interpreter is currently used by vscode, which is displayed in the lower right corner of the interface.
Clicking on the displayed python version will open a Select Interpreter panel where you can select the interpreter with the tkinter package installed to run the code
Or you can install the tkinter package for the currently used interpreter.
<path-to-current-python.exe> -m pip install tkinter
EDIT
From your terminal information it can be seen that you have activated the virtual environment, but you are running the code with Code Runner.
As a reminder, Code Runner doesn't change interpreters as you select another interpreter in the Select Interpreter panel. So please use the Run Pythonn File option provided by the official Python extension to run the code.

Related

Pylance Showing Yellow Wavy Line Under Module Name When Importing It Even Code Works Fine

Pylance is showing reportMissingImports even I installed it and code works fine.
I can run the code but it was still a very annoying nuisance.
How do I fix this?
I tried restarting Visual Studio Code and look at it again.
Didn't work.
Here is my code.
Perhaps it will help.
import cryptocode
str_encoded = cryptocode.encrypt("Encrypted Text", "p")
## And then to decode it:
str_decoded = cryptocode.decrypt(str_encoded, "p")
print(str_decoded)
Make sure you have selected the correct python interpreter.
In your picture it is clear that you have chosen python310 version interpreter, but your cryptocode package is installed under python311 version.
How to choose a python interpreter:
Click directly on the python version in the lower right corner or use Ctrl+Shift+P to open the command palette and choose Python:Select Interpreter, then choose the appropriate python interpreter.
Why did you choose the wrong interpreter and still run successfully:
You run the code directly using the python command in the terminal, your computer should have the environment variable python311 added, or other settings make it the default python version is 311.
If you run the code with the play button in the top right corner, it will use the python interpreter you chose for VS Code (in this case you will get the ModuleNotFoundError: No module named 'cryptocode' error).

How to run python script on atom?

I'm new to using atom and was wondering how to run a python script on it. I have code written at the moment which works perfectly fine in normal python shell, using tkinter, however when I run it through the command line, it says:
import tkinter as tk
ImportError: No module named tkinter
How do I fix this? In my environment variables I have already added python.exe, the file directory to the actual script I'm running and to the python download itself. How do I fix this?
The best way is to load the Jupyter plugin. It's called Hydrogen. Then under the packages menu, you can select Hydrogen/Run all and it will run your python code. there is a keyboard shortcut for doing this which speeds up the process. You can easily check your code as you write it by using the hydrogen option to run a line and go to next line.
As for your Tkinter problem have you loaded Tkinter? You can do this using pip install Tkinter. After that try running your code again.
In python 2.7 and below, the modules's name is Tkinter, while in 3.0 and above, it is tkinter.
Atom (whatever this is) seems to use a 2.7 or below version of python interpreter.

How to do debugging in a Python console without reloading the package in PyCharm?

PyCharm runs an interactive Python console (IPython in my case), but when I make changes in the code, PyCharm doesn't reimport the modules I've been editing, so the console runs the old code.
More so, if I have an old package installed via Run setup.py Task, Python imports the old one after import mymodule as mm in the console.
One workaround is to edit the code in a file and rerun it without the interactive console, but that's not a very elegant solution.
How can I keep the interactive console up-to date and update modules on-the-fly?
I'm using Python 3.4.3 in Pycharm 4.0.6 currently, and building on Anton's answer above, I add the following to my Starting script:
from importlib import reload
then you can simply use 'reload(my_module)' as you please - however this would be manual for each reload and I'm not aware of a fully automated solution if that's what you're really after.
IPython has a very nice function that recursively reloads all modules. It is called dreload, from deep reload. You just have to manually run in the console:
dreload(myCoolModule)
It is manual, but it doesn't bothers me. If I remember well, it doesn't work if you remove the namespace importing like: from myCoolModule import *

Tkinter for Windows7-64Bit

Can you please let me know how to download tkinter for windows 7 64bit. I am getting an error while i do it:
from Tkinter import *)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
from Tkinter import *
ImportError: No module named 'Tkinter'
If you are using python 3 then the code would use a lower case tkinter.
#python 2
from Tkinter import *
#python 3
from tkinter import *
if that doesn't fix it then you can install it in several ways. check out this page: http://www.tkdocs.com/tutorial/install.html
If you can't get that to work another option is using pip. pip allows you to install modules easily. If you have the newest version if python 2 then it is installed and all you have to do is set a path. Click the start button and right click on computer. Select properties and then advanced settings. Under one of the tabs is a button called environment variables. Click on it and in the new windows scroll down until you see the word "path" on the right. Click on the text to the left of it and put in a ; to put in a new path. I don't know exactly how your file structure is but find python and set a path to the folder called scripts within python. An example would be something like this: "C:\python27\scripts".
There are two possible reasons that you are not able to get Tkinter working:
First Reason
First, this is dependent on if you have installed tk onto your computer. To find out if you have, and what version, run a Python shell via:
% C:\python32\python
From the Python command prompt, enter these two commands:
>>> import tkinter
>>> tkinter._test()
This should pop up a small window; the first line at the top of the window should say "This is Tcl/Tk version 8.5"; Or whatever version has been installed on your computer. If you haven't installed, you'll want to get tkinter on there. I've found the following that may help with that:
http://www.tkdocs.com/tutorial/install.html
Second Reason
If installing tkinter onto your machine isn't the issue, then this is most likely due to what version of python you are using, as this will change some small things.
Python 3:
from tkinter import *
Python 2.7:
from Tkinter import *
If you have python you are trying to run from 2.7 or before but are using it in Python 3, you may find the 2to3 tool helpful for adapting:
https://docs.python.org/2/glossary.html#term-to3
open IDLE Python GUI and type in import tkinter and after that type import_tkinter and if you wanna run a test just type in tkinter._test()
works for python 3.4.3

Python curses Redirection is not supported

I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.

Categories