Python2 to Python3 [Small Import Issue] - python

My code is no longer working after moving from Python 2.7.9 to Python 3.4.3. I am trying to get my little program to run on my RPI and when I tried running with 2.7.9 via python server.py I get this error:
Code Issue:
def setVolume(volume):
volume = max(90, min(30, volume)) #Max Volume Spam Protection
sendPost(speakerAddress+"volume","<volume>"+str(volume)+"</volume>")

You are importing Request object and yet you are trying to
return request.urlopen(address).read()
As far as I know you can keep your old from urllib import request in the new code aswell. Here's what Python 3.4.3 docs say about it: https://docs.python.org/3/library/urllib.request.html
So you should be able to import request and parse from urllib in Python 3.4.3. If that doesn't work for you maybe consider reinstalling your Python 3.4.3 distribution.

Related

Trying to run python code on jenkins in ubuntu

all.
I recently started working with Jenkins, in an attempt to replace cronjob with Jenkins pipeline. I have really a bit knowledge of programming jargon. I learned what I learned from questions on stackoverflow. So, if you guys need any more info, I would really appreciate if you use plain English.
So, I installed the lastest version of Jenkins and suggested plugins plus all the plugins that I could find useful to python running.
Afterwards, I searched stackoverflow and other websites to make this work, but all I could do was
#!/usr/bin/env python
from __future__ import print_function
print('Hello World')
And it succeeded.
Currently, Jenkins is running on Ubuntu 16.04, and I am using anaconda3's python (~/anaconda3/bin/python).
When I tried to run a bit more complicated python code (by that I mean import pandas), it gives me import error.
What I have tried so far is
execute python script build: import pandas - import error
execute shell build: import pandas (import pandas added to the code that worked above)
python builder build: import pandas - invalid interpreter error
pipeline job: sh python /path_to_python_file/*.py - import error
All gave errors. Since 'hello world' works, I believe that using anaconda3's python is not an issue. Also, it imported print_function just fine, so I want to know what I should do from here. Change workspace setting? workdirectory setting? code changes?
Thanks.
Since 'hello world' works, I believe that using anaconda3's python is not an issue.
Your assumption is wrong.
There are multiple ways of solving the issue but they all come down to using the correct python interpreter with installed pandas. Usually in ubuntu you'll have at least two interpreters. One for python 2 and one for python 3 and you'll use them in shell by calling either python pth/to/myScript.py or python3 pth/to/myScript.py. python and python3 are in this case just a sort of labels which point to the correct executables, using environmental variable PATH.
By installing anaconda3 you are adding one more interpreter with pandas and plenty of other preinstalled packages. If you want to use it, you need to tell somehow your shell or Jenkins about it. If import pandas gives you an error then you're probably using a different interpreter or a different python environment (but this is out of scope here).
Coming back to your script
Following this stack overflow answer, you'll see that all the line #!/usr/bin/env python does, is to make sure that you're using the first python interpreter on your Ubuntu's environment path. Which almost for sure isn't the one you installed with anaconda3. Most likely it will be the default python 2 distributed with ubuntu. If you want to make sure which interpreter exactly is running your script, instead of 'Hello World' put inside:
#!/usr/bin/env python
import sys
print(sys.executable) # this line will give you the exact path to the interpreter
print(sys.version) # this one will give you the version
Ok, so what to do?
Well, run your script using the correct interpreter. Remove #!/usr/bin/env python from your file and if you have a pipeline, add there:
sh "/home/yourname/anaconda3/bin/python /path_to_python_file/myFile.py"
It will most likely solve the issue. It's also quite flexible in the sense that if you ever want to use this python file on a different machine, you won't have your username hardcoded inside.

How use suds.client library in python 3.6.2?

I run bellow code in python 2.7 it work good :
from suds.client import client
client = Client(self.service_address)
rid = client.service.bpPayRequest(terminalId=self.terminalId,
userName=self.userName,
userPassword=self.userPassword,
orderId=order_id,
amount=price,
localDate=local_date,
localTime=local_time,
additionalData=additional_data,
callBackUrl=call_back_address,
payerId=0)
But when i run in python 3.6.2,it does not work.I guess Client is for python 2.How use suds.client library in python 3.6.2 and run top code in python 3.6.2?
Try to use
$ pip install suds-py3
It works for python 3.7.

urllib vs. urllib.request in Python3 - Enthought Canopy

Getting strange difference inside Enthought Canopy vs. command line when trying to load and utilize urllib and/or urllib.request
Here's what I mean. I'm running Python 3.5 on MacOS 10.11.3. But I've tried this on Windows 10 machine too, and I'm getting the same results. The difference appears to be between using Canopy and using command line.
I'm trying to do basic screen scraping. Based on reading, I think I should be doing:
from urllib.request import urlopen
html = urlopen("http://pythonscraping.com/pages/page1.html")
print(html.read())
This works at a command prompt.
BUT, inside canopy, this does not work. Inside canopy I get the error
ImportError: No module named request
When Canopy tries to execute the from urllib.request import urlopen
Inside Canopy, THIS is what works:
import urllib
html = urllib.urlopen("http://pythonscraping.com/pages/page1.html")
print(html.read())
I would really like to understand what is happening, as I don't want my Canopy python scripts to fail when I run them outside of Canopy. Also, the Canopy approach does not seem consistent with docs that I've read... I just got there by trial & error.
urllib.request is a module that only exists in Python 3. Enthought Canopy Distribution still ships with a version of Python 2.7 (2.7.10 as of the current version 1.6.2).
In Python 2.x, you have the choice of using either urllib or urllib2, which expose functions like urlopen at the top level (e.g. urllib.urlopen rather than urllib.request.urlopen).
If you want your scripts to be able to run through either Python 3.x or in Enthought Canopy's Python distribution, then there are two possible solutions:
Use requests - this is generally the recommended library to use for interacting with HTTP in Python. It's a third-party module which you can install using standard pip or easy_install, or from the Canopy Package Index.
Your equivalent code would look similar to:
# This allows you to use the print() function inside Python 2.x
from __future__ import print_function
import requests
response = requests.get("http://pythonscraping.com/pages/page1.html")
print(response.text)
Use conditional importing to bring in the current function you need regardless of version. This is just using built-in features of Python and will not require third-party libraries.
Your code would then look similar to:
# This allows you to use the print() function inside Python 2.x
from __future__ import print_function
import sys
try:
# Try importing Python 3's urllib.request first.
from urllib.request import urlopen
except ImportError:
# Looks like we're running Python 2.something.
from urllib import urlopen
response = urlopen("http://pythonscraping.com/pages/page1.html")
# urllib.urlopen's response object is different based
# on Python version.
if sys.version_info[0] < 3:
print(response.read())
else:
# Python 3's urllib responses return the
# stream as a byte-stream, and it's up to you
# to properly set the encoding of the stream. This
# block just checks if the stream has a content-type set
# and if not, it defaults to just using utf-8
encoding = response.headers.get_content_charset()
if not encoding:
encoding = 'utf-8'
print(response.read().decode(encoding))

Cannot use suds from VS with IronPython

I'm developing a WPF IronPython application with VS 2015. I have to send a SOAP call and to read the answer so I'm trying to get suds to work. Since both pip and easy_install failed to install it, I downloaded the sources, unzipped them, and put them in the site-packages folder. It worked, now I can see them from VS.
When I try to use these modules however the program crashes, this is what I have done:
from suds import *
from suds.client import Client
class Utils(object):
def doStuff(addr, mail):
cl = Client(addr, verify=False)
cl.service.authenticate(email)
cl.service.otherFunctions(...)
I know that not every module works with IronPython, so my question is: have I made some error? Or is it suds that doesn't work with IronPython? If the second, do you know of alternatives?
Thank you.

python UDF version with Jython/Pig

When I do Python UDF with Pig, how do we know which version of Python it is using? Is it possible to use a specific version of Python?
Specifically my problem is in my UDF, I need to use a function in math module math.erf() which is newly introduced in Python version 2.7. I have Python 2.7 installed on my machine and standalone Python program runs fine but when I run it in Pig as Python UDF, I got this:
AttributeError: type object 'org.python.modules.math' has no attribute 'erf'
My guess is Jython is using some pre-2.7 version of Python?
Thanks for your help!
To get the version you are using you can do this:
myUDFS.py
#!/usr/bin/python
import sys
#outputSchema('bar: chararray')
def my_func(foo):
print sys.version
return foo
If you run the script locally then the version will be printed directly to stdout. To see the output of sys.version when you run it remotely you'll have to check the logs on the job tracker.
However, you are right about Jython being pre-2.7 (kind of). The current stable version of Jython right now is 2.5.3, so this is the version that Pig is using. There is a beta version of 2.7.

Categories