MacOS: different treatment of IDEs and terminal when running code? - python

I am having a problem with a python IDE (Sublime) on macos when trying to use ktinter:
I set up a clean mac and installed Python 3.7 via the normal installer (with the box ticked to install tkinter)
The problem I am having now is this: When I write code in Sublime and try to run it within the IDE (Sublime -> Tools -> Build) I get an error message.
Code
import tkinter as tk
tk._test()
Output
Traceback (most recent call last):
File "/Users/christiankoch/Desktop/sys.py", line 1, in <module>
import tkinter as tk
ImportError: No module named tkinter
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/christiankoch/Desktop/sys.py"]
[dir: /Users/christiankoch/Desktop]
[path: /Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
However, when I just save the file and run it in the terminal everything works fine and I see the test window.
In the terminal
python3 test.py
So ultimately I am confused about different outcomes of the two approaches. When I use Sublime on Windows it works perfectly fine; and even using pyCharm on macos works for tkinter. So I am confused what the problem is.

When you run file.py from the console, the executable as specified in the environment variables is used by default.
In sublime you probably using a build system, that points to another environment/ executable. You should have a look at https://youtu.be/xqcTfplzr7c
And follow the instructions.

Related

Module Not Found Error for 'pdf2image' in Python Script

I am working on a project to extract text from a bunch of scanned PDF's. I am following this tutorial. One of the first steps involves importing modules. I'm having some trouble importing 'pdf2image'. For context, I'm using a Conda environment called, "textExtractor" in VS Code's Python terminal. I checked if pdf2image was installed by running "Conda list" and it looks to be installed. However, when I run the python script I get an error saying,
(textExtractor)
C:\Users\mhiebing\Documents\GitHub_Repos\MonthlyStatsExtract>C:/Users/mhiebing/Anaconda3/python.exe
c:/Users/mhiebing/Documents/GitHub_Repos/MonthlyStatsExtract/PDF_to_Image.py
Traceback (most recent call last): File
"c:/Users/mhiebing/Documents/GitHub_Repos/MonthlyStatsExtract/PDF_to_Image.py",
line 1, in
from pdf2image import convert_from_path, convert_from_bytes
ModuleNotFoundError: No module named 'pdf2image'
Below is a screenshot showing pdf2image and the error:
Any idea what's going wrong?
The python interpreter you selected is not the textExtractor but the mhiebing.
You can click the Status Bar of interpreter to switch the interpreter. And you can refer to the official docs for more details.
It looks like you type the command to run the file, it's not recommended. You can click the green triangle button on the top right corner or the F5 to debug it. If you do that you can find out the truthly environment you are taking.

Can't run Python scripts in Sublime on Mac OS

Just to get it out of the way, I'm new at Python and coding in general, but very eager to learn how to do it.
The problem is that I can't run Python scripts through Sublime Text. I'm using Mac OS 10.15.7, and I've downloaded the latest version of Python from the official website and then also with Anaconda.
When I try to run any Python script in Sublime, I get the following message:
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't find '__main__' module in ''
[Finished in 0.3s with exit code 1]
[shell_cmd: python -u ""]
[dir: /private/var/folders/wh/lt9wn7qn7zl4p8xkj67bhpyh0000gn/T/AppTranslocation/86FABCDF-E2DC-4C15-A564-EFE9529B919B/d/Sublime Text.app/Contents/MacOS]
[path: /Users/alexherranz/opt/anaconda3/bin:/Users/alexherranz/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
I looked at some other similar questions here, and I installed the SublimeREPL package as many recommended, but I'm probably missing something, as I still get this error message. It's quite frustrating, as I'd like to run the scripts within Sublime instead of copying it everytime to the terminal.
Is there a fix for the problem I'm running in to? Any ideas appreciated!
You've entered code in the editor, but you haven't saved it as a .py file. Save the file, hit ⌘B, and it should run just fine.
Make sure you save the file first, before attempting to run it.
If you "save as" the file somewhere on your computer you should be able to then go into the mac version of the file explorer and then double click your file. If it is a file that opens a window, like tkinter or turtle it will run the code normally. If not it should run the code in the mac version of the command prompt. Make sure it is opening the file with python.

IntelliSense not working with Visual Studio Code/embeddable python

I want to use the embeddable version of python (3.7.0) in an windows 10 environment without need of an installer/admin permissions. Visual Studio Code is the IDE to be configured with the python interpreter above.
To install embeddable python, I followed the instructions of this answer and this issue and it works fine on CLI.
Setup VS Code according to this site:
installed Python extension and linter
added my python folder (root of python.exe) and subfolder \Scripts in PATH environment: C:/my/path/to/Python,C:/my/path/to/Python/Scripts
configured the interpreter like this in user settings: python.pythonPath": "C:/my/path/to/Python/python.exe
Sample script:
msg = "Hello World"
print(msg)
In VS Code I CAN execute the script by right click on active editor -> "Run Python file in terminal".
But I get no IntelliSense:
When I print the sys.path, it gives me
C:\my\path\to\Python\python37.zip
C:\my\path\to\Python\
C:\my\path\to\Python\lib\site-packages
Documentation for Python extension says nothing about embeddable python not been supported:
Install a version of Python 3 (for which this tutorial is written).
Options include:
(All operating systems) A download from python.org; typically use the
Download Python 3.6.5 button that appears first on the page (or
whatever is the latest version).
...
Any ideas how to solve this?
I was using the Python version from Anaconda but I was also not able to get Intellisense to display anything, similar to what you're describing.
In the end, the way that I got Python intellisense to start working in VS Code was to:
open up the Terminal (View menu => Terminal)
type conda init powershell
restart VS Code
After that, Python intellisense started working correctly.
I had the same issue of IntelliSense not working - nothing displayed and "Loading..." when actively asking for an autocomplete using ctrl + space.
I was on VSCode 1.25.1, an embeddable python 3.8.6, and the Python Extension 2018.7.1
In short, python 3.8 was not supported by the Python Extension 2018.7.1
While IntelliSense failed silently on VSCode, opening Help > Toggle Developer Tools > Console, I had this error:
Python Extension: stderr jediProxy, Error (stderr) Traceback (most recent call last):
File "d:\Users\user\.vscode\extensions\ms-python.python-2018.7.1\pythonFiles\parso\grammar.py", line 236, in load_grammar
return _loaded_grammars[path]
KeyError: 'd:\\Users\\user\\.vscode\\extensions\\ms-python.python-2018.7.1\\pythonFiles\\parso\\python\\grammar38.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\Users\user\.vscode\extensions\ms-python.python-2018.7.1\pythonFiles\parso\grammar.py", line 239, in load_grammar
with open(path) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'd:\\Users\\user\\.vscode\\extensions\\ms-python.python-2018.7.1\\pythonFiles\\parso\\python\\grammar38.txt'
Making a grammar38.txt copy of grammar37.txt is enough to fix IntelliSense in this case - to an extent. If possible, downgrading python or upgrading vscode would be better.

Run Visual Python script from bash terminal

I am trying to run the example programs that come with Visual Python ("VPython") like bounce.py, orbit.py, etc through bash.
When I'm in the folder which contains these examples (C:\Python27\Lib\site-packages\visual\examples), I can right-click on any of the examples and choose "open with" and then select "python.exe" from the list of given programs. This method is able to run the programs just fine and it generates the visual representation of a ball bouncing in a room, orbiting balls, etc.
On the other hand, when I use bash to navigate to this folder and then try to run python bounce.py, I get the error message:
Traceback (most recent call last): File "bounce.py", line 1, in
from visual import * ImportError: No module named visual
I was under the impression that calling python bounce.py was effectively the same as opening the file with the python executable but the behavior is different. I'm not sure why the "open with python.exe" method would allow the example to find the visual module but opening the example through bash is unable to find the same module. Any advice or help would be appreciated.
(Side note, I am running Bash on Ubuntu on Windows through the new beta https://msdn.microsoft.com/en-us/commandline/wsl/about)
The Linux environment (Bash on Windows) includes a builtin python, which does not have the package visual.
If you double-click the file, right-click and then open with python.exe, or execute the script via cmd or powershell, it will use the Python installed on the Windows machine. If you execute the script in bash, it will use the Python in Linux subsystem. That's it.

error in creating executable for python script

I have created a application using python2.7 also used pyqt for gui. I have also used pybel module for computations.
my application python file works properly. but when I try to create executable from that python file it gives me error.
I have tried using py2eye, cx_freeze and pyinstaller too.
I am wondering if the python file runs well after typing "python ui_MolecularTest.py" on command prompt, why I am unable to create executable from the same?
Thanks

Categories

Resources