In Python 2.7 I have the following and I debug through IDLE:
print 'Here'
import sys
reload(sys)
sys.setdefaultencoding('cp1252')
print 'There'
what I get in return is
Here
So after I have set the default encoding it does not print the desired output.
Could this be due by conflicts with the IDLE encoding?
Because it is unable to find reference to setdefaultencoding from sys. That is why it is not printing 'There'
setdefaultencoding is deprecated and one should never use it!
Have a look at the following link.
Why should we NOT use sys.setdefaultencoding(“utf-8”) in a py script?
Related
I have multiple python scripts, each with print statements and prompts for input. I run these scripts from a single python script as below.
os.system('python script1.py ' + sys.argv[1])
os.system('python script2.py ' + sys.argv[1]).....
The run is completed successfully, however, when I run all the scripts from a single file, I no longer see any print statements or prompts for input on the run console. Have researched and attempted many different ways to get this to work w/o success. Help would be much appreciated. Thanks.
If I understand correctly you want to run multiple python scripts synchronously, i.e. one after another.
You could use a bash script instead of python, but to answer your question of starting them from python...
Checkout out the subprocess module: https://docs.python.org/3.4/library/subprocess.html
In particular the call method, it accepts a stdin and stdout which you can pass sys.stdin and sys.stdout to.
import sys
import subprocess
subprocess.call(['python', 'script1.py', sys.argv[1]], stdin=sys.stdin, stdout=sys.stdout)
subprocess.call(['python', 'script2.py', sys.argv[1]], stdin=sys.stdin, stdout=sys.stdout)
^
This will work in python 2.7 and 3, another way of doing this is by importing your file (module) and calling the methods in it. The difference here is that you're no longer running the code in a separate process.
subroutine.py
def run_subroutine():
name = input('Enter a name: ')
print(name)
master.py
import subroutine
subroutine.run_subroutine()
On repl.it, I tried to call the path I know it exists, but it gave me an error.
Code:
import os
print os.path.exists('C:\Users\')
Error:
SyntaxError: EOL while scanning string literal
exited with non-zero status
You need to escape your backslashes. See the documentation here.
import os
print os.path.exists('C:\\Users\\')
This question already has answers here:
IPython Notebook locale error [duplicate]
(4 answers)
Closed 6 years ago.
After updating IPython I constantly have problems with matplotlib. At the beginning of my notebook I have
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import scipy
from qutip import *
import time
Which generate a screen full of issues but the final part is
/Users/murray/anaconda/lib/python2.7/locale.pyc in _parse_localename(localename)
473 elif code == 'C':
474 return None, None
--> 475 raise ValueError, 'unknown locale: %s' % localename
476
477 def _build_localename(localetuple):
ValueError: unknown locale: UTF-8
There were other issues before this which I managed to fix. Similar things have been reported here but no solution which works for me. One solution I found online suggested running
export LANG="it_IT.UTF-8"
in the terminal window (plus about 8 other similar commands). This worked but everytime I restart the notebook I have to reenter all of this. As you might guess I am not an expert - I would assume there is a more permanent fix for this problem
As a work around, you can put export LANG="it_IT.UTF-8" and the "8 other similar commands" into your .profile (assuming your are on Mac OS X).
At the end of this file /Users/murray/.profile write:
# Fix for matplotlib imports in IPython
export LANG="it_IT.UTF-8"
# your other 8 lines here without the # in front
You need to start a new terminal window. In there, start a new IPython session.
When printing nothing but
print ('\033[0m')
the system exits without any warning. I.e. '\033[0m' must follow a newline in the passed string, or be printed by itself.
I am using the print function so have included;
from __future__ import print_function
Looking at the web help;
https://docs.python.org/release/2.7.6/library/functions.html?highlight=print#print
it does not indicate any exceptions thrown for this function (I am assuming this is the function and not statement documentation).
My question is why does this barf when passed this line and is there a later fix for it?
Thanks.
OK so I am trying to run one python script (test1.py) and print something into a webpage at the end. I also want a subprocess (test2.py) to begin during this script. However, as it happens, test2.py is going to take longer to execute than test1. The problem that I am having is that test1.py is being held up until test2 completes. I have seen numerous people post similar questions but none of the solutions I've seen have fixed my issue.
Here's a heavily simplified version of my code that demonstrates the issue. I am running it on a local server, and displaying it in firefox.
test1.py:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import cgi
import cgitb; cgitb.enable()
import subprocess
form = cgi.FieldStorage()
print("Content-Type: text/html") # HTTP header to say HTML is following
print # blank line, end of headers
p = subprocess.Popen(['python','test2.py'])
print "script1 ended"
test2.py:
from time import sleep
print "script2 sleeping...."
sleep(60)
print "script2 ended"
Essentially I want to execute test1.py and have it say "script1 ended" in firefox, without having to wait 60 seconds for the subprocess to exit. I don't want anything from the test2 subprocess to show up.
EDIT: For anyone interested.. I solved the problem in windows by using os.startfile(), and as it turns out subprocess.Popen([...], stderr=subprocess.STDOUT, stdout=subprocess.PIPE) works in Linux. The scripts run in the background without delaying anything in the parent script when I do a wget on it.