Module Error, not able to run python script in cmd line - python

I'm having problems running my python script on the cmd line and am running into a module error (1st screenshot)
What I done so far was go into path environments. After that I created a variable, under system variables, that contains the below (2nd screenshot).
I then created a variable called: %PYTHON_HOME% in the variable called Path (also under system variables).
So far I have no problems running the script in Atom but if I were to run this in cmd prompt I can't get it to run. Would anyone have any advice on what else I could do?
Some additional things I had done:
I entered the below in Atom to see my sys.path.
import sys
for i in sys.path:
print(i)
Output
C:\Users\JIMMY-PC\Desktop\PythonNotes
C:\Users\JIMMY-PC\Desktop\Python Notes
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39
C:\WINDOWS\system32
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\python39.zip
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\DLLs
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\lib
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\win32
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\win32\lib
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\Pythonwin
UPDATE2:
I tried running my python script in cmd line. Looks like what's appearing in Atom and comparing to cmd line contains different python version. Not sure how to fix this so it looks at python 3.9 instead of 3.7. Any advice would be much appreciated!

Found the answer! Will leave my solution here in case other people have this problem....
Because I have different version of python in my computer, that was the reason my computer was having trouble finding the correct module path. I decided to go over to control panel>programs>uninstall a program> all python versions except for the latest version.
As for path environments I created PYTHON_HOME under system variables with the two paths inside (screenshot below). Then I plugged in %PYTHON_HOME% inside Path, restarted my comp and now I can run my python scripts from both my Atom IDE and cmd prompt!

Related

python is not recognized as an internal or external command, operable program or batch file

I realize that a lot of people have had this question but I've tried their solutions and they haven't helped me. I watched this video here but his solutions is not helping me:
fix problem
I can get python to work on the python app and using it the path of the python interpreter is
import sys
print (sys.executable)
>>> C:\Users\Owners\Documents\Python
I then set the environment variable like so:
I've also tried attached 'python.exe' but that didn't work either. When I then write python in the terminal I get the error message in the terminal.
You should make a new environment variable in your system variables named PATH if it doesn't already exist then you need to add your python interpreter path to that variable.
Adding Python.exe file in Path won't work as it is just an executable file, also which version of Python have you installed because according to this python 3.9.0 and above won't run on Windows 7 or earlier.If this is not applicable in your case then click on New in your system variables menu and add PATH as name and locate the Python Application Path and Python Scripts Path in folders (i.e where you installed Python) and add it to the newly created PATH

Can't access local programs from Python script

I´m trying to execute locally installed programs from a Python script (OSX), but they are not found, since /usr/local/bin is not in the PATH. Running os.environ gives only /usr/bin:/bin:/usr/sbin:/sbin.
It is probably a common/simple problem, but I´ve exhausted Google, and start feeling a little stupid :-)
How do you try to execute your programs (please provide us a minimal code sample)?
If you are using the subprocess package, you can try to provide the full path of your executable:
subprocess.run(["/usr/local/bin/my_program"], ...)
Else, you can try to append /usr/local/bin to the os.environ list.

Python File Directory Can't be found

When I try to run my python file, I get the following error: "can't open file 'hello.py': [Errno 2] No such file or directory" I have tried cd and it shows that my file is in the Users/ierdna/ directory. I have the python program on my desktop and I still cannot run it.
Thanks very much!
It seems that I have tried everything, and nothing is working. :(
I am going to assume you know some of the basic BASH command line commands. If you don't check them out here.
Opening your terminal's respective shell, enter the following on your command line:
cd Desktop [to change directory to your desktop]
ls [to list all the directories and files on your desktop, to make sure your hello.py file is in fact there]
python hello.py [to run your python file]
That should run it. Let me know if you run into errors.
In order to properly answer this question the following information are required:
a. OS that you are using
b. Release version of that OS
c. Python version that you are using
d. If your machine has at least 10GB of free space
Kidding!
You just need to use cd ~/Desktop to make the 'Desktop' your working directory and then try to run python hello.py Alternatively you can also try running python ~/Desktop/hello.py directly without using 'cd' command. Note: In order to run a python script you need to provide the path(Either complete path, for example: python /home/username/Desktop/script.py or relative path, for example: python ../script.py) to the script. If you just provide the script name, it will fail unless the script exists in the current working directory. Also, kindly do check for existing questions and answers before posting your own question as I doubt this question is new and hasn't been answered correctly before.
I'm going to assume from you saying you used cd that you are using Mac or Linux. This solution will work for both. If I am wrong and you are running Windows, just comment it and I'll change the answer. On to the real answer:
First open your terminal, then type cd ~/Desktop. Now try running your python script.
EDIT:
Apparently you are running Windows. OK. I'm going to leave the above answer for other people who have the same problem on Mac or Linux. What you need to do is execute this command in your command prompt cd C:\Users\[your user name]\Desktop. Replace [your user name] with your actual user name. Then run your python script (python hello.py)

When using Python Windows Launcher, is there any way to prevent having to type full path?

In Windows 8, I often use the Python Windows Launcher like
py C:/long/long/long/long/long/path/to/prog.py ...
Is there any way to set some environment setting, such as PATH or PYTHONPATH etc, to prevent having to type the full path to prog.py?
From my basic knowledge/research, PATH only helps with the py part of the command line and PYTHONPATH only helps with imports within prog.py, so how do I deal with the path to prog.py itself??
Notes:
I cannot modify the code, not even the "shebang" line, since it is needed to work on other platforms.
I cannot cd to the directory containing the programs to run them, because the programs will do something based on the directory they're run in (they'll modify the files in the directory they're run in).
I know that if I associate .py extension with the Python Windows Launcher, then I can run prog.py as the first item in the command line, and thus use PATH, but currently my .py extension is associated with my favorite editor and I'd like to keep it that way if possible (so I can double-click any Python file in Windows Explorer and edit it).
However, if someone suggests a solution where I can have a different association for Windows Explorer versus the command line, then that could be a potential solution! (i.e. in Windows Explorer, .py opens with the editor, while on command line, .py runs with Python Windows Launcher)
Add your long path to PYTHONPATH, then invoke your program as such:
python -m prog
Python will search for a module called prog and then run it as the main module.
Answer to my own question: Actually, I'm so silly. I could just set a variable for each program path (there are only a few programs paths), i.e.. prog=C:/long/path/to/prog.py and then do py %prog% .... I guess I figured out an answer to my own question that was acceptable to me.
Update: I just found something even better. I can do
doskey prog=py C:/long/path/to/prog.py $*
and then simply prog ... afterward
Now I just have to do some crazy stuff to get the doskey command into a file that will be run every time I start a console, as described here: https://stackoverflow.com/a/21040825/5182136

Setting up a Python development environment on Windows

Yes, I've searched. So after spending about 4-5 hours struggling just to get Python files running, I recently stumbled over the solution to get it running through the environment variables like this: cmd -> python -> Python starts, yay yay
Since it didn't work to do it through the command line and similar I had to do it manually through the Windows interface. Now that it's working, however I cannot open .py files without typing out the full path like this: python C:\X\X\X\test.py which is obviously also starting to get annoying.
So now I'm trying to find out which variable I have to change (yet again) to only be able to type 'python test.py' and have it running. Sorry if I come off vague, but it's always a major pain to setup a new programming language for me and it kills my mood.
Thanks for help, it'll be really appreciated.
When you say
able to type 'python test.py'
I'm not sure exactly what you mean. Normally when the Python interpreter runs, it looks in the current directory for any source file that is named on the command line (unless you specifically name a location for the source file, as you've discovered). It seems from your previous statement:
python C:\X\X\X\test.py which is obviously also starting to get annoying
that your test.py file exists somewhere else.
What you might want to try is to change the current directory first, before running your script. In a command prompt window, type:
C:
cd \X\X\X
python test.py
(obviously substituting your actual path name). My apologies if you already know this.
To make python executable on your command line, you need to add it to your PATH environment variable, which it sounds like you have done on the command line. It is quite simple to add directories to the PATH in Windows if you know where to look. Essentially, you need to get to the Environment Variables dialog box, which is slightly different for each version of Windows.
For Windows XP: Start -> Control Panel -> System -> Advanced -> Environment Variables
For Windows Vista, 7: Click the Start Orb, right-click Computer and select Properties -> Advanced -> Environment Variables
Then, in the lower of the two boxes, find Path and click Edit. Change it so that C:\Python27 (or whichever version of Python you have) is at one end of the list, separated from the other entries by a semicolon (e.g. C:\Python27;C:\Program Files ...)
Once you've done this, python will work at the command line whenever you open a command window.
Regarding your second issue, however, there isn't much you can do. You must either specify the complete path to your script or already be in the same directory as the script. That is, if the script is in C:\X\X\X you will either need to invoke it as C:\X\X\X\test.py or first cd C:\X\X\X.

Categories