Pycharm does no read python file without error - python

When I create a project and I run the script which should print something but I got nothing:
C:\Users\mahfo\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/mahfo/PycharmProjects/pythonProject1/main.py
Process finished with exit code 0
main.py file content:
print('wef')
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
When I check: sys.prefix == sys.base_prefix, output is: True,
and sys.prefix output:
'C:\\Users\\mahfo\\AppData\\Local\\Programs\\Python\\Python38-32'
It is the first time I try on Windows. Do you know why this happens, please? Any help is appreciated.
Edit:
I've tried to run the file directly from the command prompt and it run correctly with python but not python3. So Pycharm does not recognize the file (without error) because the file can be read only with python2. I don't understand why because the pycharm environment setting are on python3.
Edit 2:
I had Python 2.7 installed that i have deleted.
Pycharm is still not printing when I run but it print on debug console. I really don't understand why.

Related

script run from IDE, but not from git-bash terminal

I am running a python script from the IDE and it works well, however when calling it from the git-bash terminal the following errors showed.
./main.py: line 2: import: command not found
./main.py: line 3: import: command not found
./main.py: line 5: syntax error near unexpected token `'I am in main 1''
./main.py: line 5: `print('I am in main 1')'
In git-bash I run the chmod u+x main.py and chmod +x main.py lines, but it still does not work.
I am using VScode as my IDE with python3.7.9 as the interpreter, I am calling the script from the direction where it is placed in the terminal, and the script that I am testing is the following:
#!/bin/bash
import os
import sys
print('I am in main 1')
I have changed the first line for #!/bin/bash/python and #!/bin/bash/env python3... didn't work. I would appreciate it if someone could tell me what I miss or what I am doing wrong. I am new in git or using the terminal, any tip would be appreciated.
Thanks in advance.
Not sure if you made the error in typing this question or if it's also like this in your script, but you should have #!/usr/bin/python3 or #!/bin/python3 for your system to know to execute it as a python script.
As it stands, your system is trying to execute it as a shell script with bash, which would explain why the import statements aren't being recognized.

Python script doesn't print output in command prompt

I need some advice with a Python script. I'm still new and learned it by myself. I found the script on Google. After I retype it, it doesn't print the result in the console. How can the result of the script be shown in the console? Details as below:
C:\Python27>test1.py af8978b1797b72acfff9595a5a2a373ec3d9106d
C:\Python27>
After I press enter, nothing happens. Should the result be shown or not?
Here's the code that I retyped:
#!/usr/bin/python
#coding: ascii
import requests
import sys
import re
url = 'http://hashtoolkit.com/reverse-hash?hash='
try:
hash = sys.argv[1]
except:
print ("usage: python "+sys.argv[0]+" hash")
sys.exit()
http = request.get(url+hash)
content = http.content
cracked = re.findall("<span title=\*decrypted (md5|sha1|sha384|sha512) hash\*>(.*)</span>", content) # expression regular
print ("\n\tAlgoritmo: "+cracked[0][0])
print ("\tPassword Cracked: "+cracked[0][1])
The first line in your script is called a Shebang line.
A Shebang line tells the script to run the Python interpreter from that location.
The shebang line you provided is a Linux system path, but it looks from the path you are executing Python from, that you are running on Windows.
You can do one of two things here to fix that:
Remove the Shebange Line.
Remove the first line from your script.
Execute the script using python test1.py COMMAND_LINE_ARGUMENTS
Modify Your Shebang line.
Change the first line of your script from !/usr/bin/python to
#!python (This is assuming that python is in your systems PATH variable.)`
Execute the script using test1.py COMMAND_LINE_ARGUMENTS
Also, you are trying to import the requests module that is not installed in the standard library.
If you haven't installed this yet, you can do so by going to your Python install directory and go to the scripts folder.
Hold shift and right click and go Open command window here
Type pip install requests and hit enter.
After that you should be good to go, execute the script by navigating to it and type test.py COMMAND_LINE_ARGUMENT
If a Python script doesn't have the shebang line:
python test.py COMMAND_LINE_ARGUMENT
you need to run your script using python. try:
C:\Python27>python test1.py af8978b1797b72acfff9595a5a2a373ec3d9106d

How does IDLE run a .py module?

Windows 7 and Python v2.7.3
With this module "test.py":
print 'name: ' , __name__
print 'file: ' , __file__
I can go to CMD and type: python test.py
The result would be:
name: __main__
file: test.py
But if I open the file with IDLE and press F5 to run the program I'll get a NameError telling me __file__ is not defined.
What steps does IDLE go through to run a .py module, and how is it different from running modules directly from command line?
That's a bug, which has apparently (I don't use IDLE) now been fixed - the discussion of the bug at that link has some explanation of what IDLE actually does to run your code.

python command line ok in python shell but not through windows cmd

I was about to test the ftpmirror builtin script (python322, winXP 32bits) from the cmd windows default shell and get this :
File "C:\Program Files\python322\Tools\Scripts\ftpmirror.py", line 161
print('Skip pattern', repr(pat), end=' ')
^
SyntaxError: invalid syntax
I tested the print() line directly in the python shell, trough cmd, and with idle (and in blender also) : this work obsiously.
I reproduce the error with a coucou.py file like this :
#! /usr/bin/env python3
pat = 'toto'
print("Skip pattern", repr(pat), end=" ")
when directly called from a cmd prompt :
C:\Program Files\python322\Tools\Scripts>coucou.py
same error than with ftpmirror
but :
C:\Program Files\python322\Tools\Scripts>python coucou.py
is ok
and my environment is ok I can execute py scripts directly from the windows ui by double-clicking a .py file, and I got working scripts working fine when called from .bat
I don't get it, it looks specific to the print() end argument, what did I not read yet about the way to execute python3 from the windows cmd shell ?
thanks,
Jerome
Try checking if you are running the same python interpreter when you double click or you run python from the command-line.
Save this in a .py file with this content and try running it with both methods:
import sys
print sys.version_info
I bet you are using different interpreters in each case.

Why different behaviors between running Python module as an executable vs. Python [filename]

So, I created a simple python module, test.py
import commands
def main():
cmd = 'ls -l'
(status, output) = commands.getstatusoutput(cmd)
print status, output
if __name__ == '__main__':
main()
When I ran it using "Python test.py", I got the result that I expected. But when I ran it as an executable (yes, it has the 'x' permission), the program didn't respond at all and I had to Ctrl+C to quit it. Why is that? Shouldn't both ways give the same result?
Add a hash-bang line to the top:
#!/usr/bin/env python
import commands
...
This tells your system what interpreter to use to execute the script. Without it it doesn't know if it's a shell script, Perl script, Python script, what.
You need the hashbang to be the first line of your script, referencing the path of the Python interpreter. Otherwise, all the OS knows is that you're trying to execute a script, and it has no idea how to go about doing that.

Categories