Why Pycharm can find my module,but can't run it? - python

I am a novice about Python and Pycharm. I followed the tutorial wrote the code below:
This code can run well on IDLE. But can't run in Pycharm.
I searched online. The answers I found are about Pycharm interpreter setting. My interpreter setting is good. It can find the module. But when I run it, There are exceptions.
I spent several hours try to solve this problem, still don't know why.

Change your filename to somethingelse.py
EDIT (with a better explanation:)
What is happening here is that OP is trying to import tkinter. However, since they have named their file "tkinter.py," the interpreter thinks that this is the module OP is referencing. For this reason, OP is not getting the desired results, and instead is getting an error about missing attributes.
This probably worked for OP in the IDLE environment, because IDLE allows you to execute the code from within the environment as if you were in a terminal. Pycharm was trying to have the Python interpreter execute the .py file that OP had pulled up.
More information about how Python's import system works can be found here: https://docs.python.org/3/reference/import.html

Related

VS Code can't find Python and interpreter

I searched everywhere, but did not find an answer. Since I think the answers are outdated because they use python.pythonPath and this does not exist anymore.
My problem is the following:
But I installed Python#3.10 already via homebrew (I have a mac M1 with macOS Monterey).
If I use ,,which python3'', I will get:
And here you can see the output of ,,echo $PATH'':
What should I do, so that the error message does not appear again??
You have to click on 'No Interpreter Selected' and then you can add a path to python
If nothing else will work
Go on terminal and write your python-path\python.exe python-file.y.
If this works you can create a shell script inside of your folder and then put inside the python call. Then you can start your python file with the shell script.
You can go to the Extensions Library and download the Python Interpreter for VS Code. Make sure Python is installed on your PC.
If you still have problems, try reinstalling Python and VS Code.
I think the above two answers have been well analyzed. If you can't solve the problem, you can refer to the instructions of vsocde and reinstall python.

Why does my VS Code not recognize 'python' as an internal or external command

so I just considered using VS Code for writing python as i'm settling into programming. After following the full guide here: https://code.visualstudio.com/docs/python/python-tutorial and everything seemed ok.
But my code wont run, but runs well in my PyCharm but on VS Code it pops this error below:
"'python' is not recognized as an internal or external command,
operable program or batch file."
I have followed threads here advising reboot of computer and all but its not working for me, I would love to at least know what the problem is because my decision to use VSC is because i'd like to have one IDE that supports more languages so that write in one place or at least a few IDEs.
Any help will be appreciated.
Thanks in advance.
thanks, I've fixed it by uninstalling the existing python and reinstalling latest version, most importantly i realized i was adding a new path instead of editing the existing path. In recent windows clicking edit on the existing path will open a new dailog where you can then add a new path, that will be added to the existing path. Then I restarted my computer and then typed python --version at command prompt and there it was :) by the way this link (geek-university.com/python/add-python-to-the-windows-path) was most helpful but you have to do it as i described.

Python fails silently

I am trying to get started with Python, so I installed Python 3.8, from python.org (on Windows 10). I remembered to check the "Add to PATH" during the installation and I have confirmed that it has been added to path, as seen in the first image.
However, when I try to use any commands, whether it be python --version or python HelloWorld.py, nothing happens. I have tried both the traditional command line as well as Powershell. I have also tried replacing python with python3, the result is the same. As you can see in the second image, I get no errors, just a blank line. So it's not that it cannot find Python at all, it rather seems that something is wrong with the installation.
Have anyone else encountered this kind of behavior? I have tried re-installing Python as well as removing old installations.
Image 1: Python seems to be correctly added to PATH.
Image 2: Python fails to return any output. But also no error. Same thing happens with python HelloWorld.py.
Update 1 - Here is what I have tried so far:
Changing PATH to refer directly to exe file.
Using both Command Prompt and PowerShell.
Rebooting the PC.
Re-installing Python (including removing old versions).
Both the python and python3 command.
Update 2 - NameError: name 'python' is not defined
Update on the update: This was a wild goose chase. You are not supposed to be able to use that command in the interpreter, as described in this post.
So I tried to use the console from the python.exe file instead, and I got the following answer. However, from what I can find on it, it's normally a problem you encounter on elements of the code (like print()) and not on the python command...?
(image removed)
Update 3 - It works (kinda)
If I use the command py --version or py test.py it works. I have no clue why though...
After extensive research, I still cannot find an answer to my own question. But I have found a work around, which minimizes the consequences:
Instead of using python, simply use py.
This will invoke the Python launcher instead of Python itself (from what I've read). For most people this will be good enough, but it is not the same. So it might cause issues and version mismatch in certain scenarios.
Also, despite of this weird behavior, Python seems to run fine in Visual Studio Code, when using the "Run" button (or the py command in the terminal).

Tensorflow - running python files in Spyder

to give a bit of background I'm trying to use TensorFlow object detection api. I have been following this tutorial https://www.youtube.com/watch?v=MoMjIwGSFVQ
15 minutes into it he uses Spyder, and runs the python files on it. I've never used Spyder before, and for some reason it's not running my files at all? When I press run nothing is returned on the console.
So can anyone suggest a different IDE that I could use that would allow me to do the same things as he does? Or would anyone be able to tell me why Spyder might not be running. (I installed it in Linux terminal, and simply imported the file as a project as he did, and so I'm a bit stumped as to whats going wrong).
Thanks!

Issue with Python Batch file to run Python through Notepad++

EDIT: The code I wrote in my Python file was just this:
print "foo"
I'm using Windows XP Home Premium on this tiny little HP Mini 1000, and I want to run Python files, since we're learning it in school. I am aware of this topic, so I tried to run Python files using a batch file (python.bat), and I'm getting an error that says, "Can't find 'main' module in ''" whenever I run the batch file. I followed the instructions given here. All I did was change "Python26" to "Python33" because of the difference in versions.
Any idea what's wrong here? I really want to run Python files from Notepad++, so I don't want any alternative ways to run them.
This sounds like you don't have PYTHONPATH set up correctly. I suggest you review the documentation here:
http://docs.python.org/2/using/windows.html
Instead of calling Python, call cmd.exe and then use the set command to inspect which variables are set and how they are set. Run the exit command to leave the command shell. When you think you have the variables set up correctly, try again to run Python.
Good luck and have fun!
I use the command line interpreter or IDLE mostly (Win 8.1 now, but I've done so since Win XP SP2), but NPP is my main text editor, so I was curious about this issue.
When I was reproducing this, I was able to generate several errors, but the only one I got that was an exact match was when I failed to configure the Run option correctly.
You need to make sure to follow this step exactly in the instructions you were following. When you navigate to Run -> Run in Notepad++, you have to enter this exactly:
C:\Python33\python.bat "$(FULL_CURRENT_PATH)"
I am pretty sure you left out the "$(FULL_CURRENT_PATH)", or otherwise didn't add it correctly, as failing to do so causes exactly the same error on my end. Failing to include this means that when you run the batch script, you get the wrong input to the Python interpreter, causing the error.

Categories