print in a loop Python Windows

811 views Asked by At

I noticed something strange when using the Django shell on Windows. I tested different calls with a simple script which is :

for it in range(5): 
    print(it)

When typing this script in a Python environment or a manage.py shell one, it works fine. But when I put this code in a file and execute it with python manage.py shell < myFile.py there's nothing which is displayed when I'm printing in loops, the output is "..." instead.

I overpassed this restriction by executing it in the build-in console from Eclipse but I'm pretty curious of the reasons why the output isn't well displayed when calling the script from an output file.

Does anyone knows what happened ?

3

There are 3 answers

0
alecxe On BEST ANSWER

It works this way:

echo 'execfile("myFile.py")' | python manage.py shell
0
Kobz On

Both worked fine

In addition, for those who are using Python 3+, execfile doesn't work anymore, replace

echo 'execfile("myFile.py")' | python manage.py shell

with

echo exec(open('MyFile.py').read()) | python manage.py shell

Thanks for both of you!

1
Nilesh On

try this

echo "for i in range(5): print i" | python manage.py shell