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.
Related
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.
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 *
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.
I am using Python 3.3 (2.7 is also installed) and a compatible version of pygame. Recently I have been trying to switch from IDLE to Notepad++
I am using a saved shortcut in Notepad++
C:\Python33\python.bat "$(CURRENT_DIRECTORY)" "$(FILE_NAME)"
which runs the batch file:
#echo off
cd %1
%2
if not errorlevel 1 goto quit
echo.
echo.
pause
:quit
When I run C:\Python33\Foldername\imp_prob.py
import pygame
in IDLE it works fine, in Notepad++ using that shortcut it gives an ImportError: No module named pygame
My questions are:
Why is the NP++ method not producing the same result?
How can I change the shortcut or batch file to make it run stuff that IDLE can run?
What method can I use to ensure that I can import a module regardless of which directory I am running the program from?
edit: a working alternative was in the answers to How do you run a python script from within notepad++?
I had some issues with the code they provided, but replacing "python" with the full path to my python33 install solved that.
I still don't understand why pygame wouldn't import when using my run shortcut. I also don't understand why NppExec works when Run doesn't.
It sounds like you would need to set your systemvariables. Idle does not require these steps. You entered the full path to the python.exe in np++ to execute the python program, but the path to the modules etc. is still unknown.
Add the paths, and try again.
System Properties -> Advanced -> Environment Variables, in the bottom window look for a "Path" variable, Edit and append the following the existing entries (do not delete anything in there!)
;C:\Python33;C:\Python33\DLLs;C:\Python33\Lib
for Python 3.3, if you have installed it into its default directory.
To see if everything worked, open the console anywhere (shift+rightclick -> Open Command Window Here) and just type "python". The python console should open, telling you that you use python 3.3. You then also do not need to tell np++ the full python path, but instead can just use "python" again.
I installed Blender 2.6 and I'm trying to run a script called drawcar.py (Which uses PyOpenGL)
I looked around the documentation for importing a script and could only access Blender's python console.
How do I run drawcar.py from the Linux terminal with Blender?
You can also execute the following code in the python console to execute an external script without opening it up in the text editor:
filename = "/full/path/to/myscript.py"
exec(compile(open(filename).read(), filename, 'exec'))
The above code comes from the following link:
Blender - Tips and Tricks
Open a Text Editor view in Blender.
Press Alt + O, or go to Text>Open Text Block and open the .py file
Then simply press Run script :D
P.s. Instead of opening a file in step 2, you can also hit the "+ New" button and create a new script instead.
Note : In newer versions the Run Script button label has been replaced with a Play icon :
this answer is too late, but to help anyone with the same problem
via the terminal:
blender yourblendfilenameorpath --python drawcar.py
from the man pages
-P or --python <filename>
Run the given Python script file.
To run a script by another script or from console:
import bpy
script = bpy.data.texts["script_name.py"]
exec(script.as_string())
It is likely that drawcar.py is trying to perform pyOpenGL commands inside Blender, and that won't work without modification. I suspect you are getting some import errors too (if you look at the command console). Blender has it's own internal python wrapper for opengl called bgl, which does include a lot of the opengl standards, but all prefixed by bgl.
If you have a link to drawcar.py I can have a look at it and tell you what's going on.