ImportError: cannot import name 'Random' in pycharm - python

I write a simple python in pycharm:
import requests
req = requests.get("http://phika.ir/")
print(req)
req = requests.get("https://phika.ir/python")
print(req)
but in result I came up with:
random
from random import Random as _Random
ImportError: cannot import name 'Random'
as you see, I didn't use random function!

I found the problem!!! I have saved a python file named random.py in current directory. so, I changed the name of this file and the problem solved.

Related

global name 'json' is not defined

Here is the beginning of createPeliMelo.py
def creation(path,session):
myPathFile=path+session+'.txt'
print myPathFile
pelimeloFile = open(path+session+'.txt', 'r')
with pelimeloFile as inf:
data = json.loads(inf.read())
Here is my Python script inside Maya:
import maya.cmds as cmds
import json
import os
from itertools import islice
import createPeliMelo as PeliMelo
PeliMelo.creation('C:/Users/francesco/Desktop/pelimelo video printemps/','session5723')
Here is the error I got:
Error: line 1: NameError: file C:/Users/francesco/Documents/maya/2016/scripts\createPeliMelo.py line
17: global name 'json' is not defined #
Line 17 is: data = json.loads(inf.read())
Where am I wrong?
When you import something, that import only applies to the file that you imported it in. This means that if you want to use json in createPeliMelo.py you need to do import json in THAT file, not your second script. Imports from one file will not propagate over to another.

Reading this type of Json with Python 3 Urllib

My json url has this:
{years=["2014","2015","2016"]}
How can I get this strings from URL with Python 3? I know this method but Python 3 has no urllib2 module.
import urllib2
import json
response = urllib2.urlopen('http://127.0.0.1/years.php')
data = json.load(response)
print (data)
ImportError: No module named 'urllib2'
Try changing the import to urllib, and use urllib.request instead. For the reason being, please refer to this SO Answer
import urllib
import json
response = urllib.request.urlopen('http://127.0.0.1/years.php')
data = json.load(response)
print (data)

Python : Function to pull a sound clip from URL and save it in local machine

Would like to create a function that pulls a sound from given url and saves it in my machine locally
use urllib module
import urllib
urllib.urlretrieve(url,sound_clip_name)
the file will be save as what you provide the name
alternative, using urllib2
import urllib2
file = urllib2.urlopen(url).read()
f = open('sound_clip','w')
f.write(file)
f.close()
don't forget to give the extension of your file
If in Python 2.7, urllib2 module is your friend, or urllib.request in Python3.
Example in 2.7 :
import urllib2
f = urllib2.urlopen('http://www.python.org/')
with open(filename, w) as fd:
fd.write(f.read)

bdecode Library in Python doesn't work

I'm trying to decode a bencode format using the bdecode library in python. I have imported the bcode library as well in my python folder. When i try to use the function bdecode which is defined in the library. I get an error
File "C:\Python27\fit.py", line 21, in <module>
decoded = bdecode(data)
NameError: name 'bdecode' is not defined
Any idea why this error is happening, I'm just new to python? If this is because of the bcode library , could anyone submit a link to some other bcode library?
This is the code I'm trying
import bcode, urllib, urlparse, string
url = "http://update.utorrent.com/installoffer.php?"
url = url + "offer=conduit"
filename = "out_py.txt"
urllib.urlretrieve(url,filename)
with open ("out_py.txt", "r") as myfile:
data=myfile.readlines()
decoded = bdecode(data)
You can solve this one of two ways, change your import statement:
from bcode import bdecode
import urllib, urlparse, string
Or change the line where you call the function:
decoded = bcode.bdecode(data)
The issue is that while you were importing the bcode module, you were not importing any of the symbols within it in to the local namespace.

With regards to urllib AttributeError: 'module' object has no attribute 'urlopen'

import re
import string
import shutil
import os
import os.path
import time
import datetime
import math
import urllib
from array import array
import random
filehandle = urllib.urlopen('http://www.google.com/') #open webpage
s = filehandle.read() #read
print s #display
#what i plan to do with it once i get the first part working
#results = re.findall('[<td style="font-weight:bold;" nowrap>$][0-9][0-9][0-9][.][0-9][0-9][</td></tr></tfoot></table>]',s)
#earnings = '$ '
#for money in results:
#earnings = earnings + money[1]+money[2]+money[3]+'.'+money[5]+money[6]
#print earnings
#raw_input()
this is the code that i have so far. now i have looked at all the other forums that give solutions such as the name of the script, which is parse_Money.py, and i have tried doing it with urllib.request.urlopen AND i have tried running it on python 2.5, 2.6, and 2.7. If anybody has any suggestions it would be really welcome, thanks everyone!!
--Matt
---EDIT---
I also tried this code and it worked, so im thinking its some kind of syntax error, so if anybody with a sharp eye can point it out, i would be very appreciative.
import shutil
import os
import os.path
import time
import datetime
import math
import urllib
from array import array
import random
b = 3
#find URL
URL = raw_input('Type the URL you would like to read from[Example: http://www.google.com/] :')
while b == 3:
#get file name
file1 = raw_input('Enter a file name for the downloaded code:')
filepath = file1 + '.txt'
if os.path.isfile(filepath):
print 'File already exists'
b = 3
else:
print 'Filename accepted'
b = 4
file_path = filepath
#open file
FileWrite = open(file_path, 'a')
#acces URL
filehandle = urllib.urlopen(URL)
#display souce code
for lines in filehandle.readlines():
FileWrite.write(lines)
print lines
print 'The above has been saved in both a text and html file'
#close files
filehandle.close()
FileWrite.close()
it appears that the urlopen method is available in the urllib.request module and not in the urllib module as you're expecting.
rule of thumb - if you're getting an AttributeError, that field/operation is not present in the particular module.
EDIT - Thanks to AndiDog for pointing out - this is a solution valid for Py 3.x, and not applicable to Py2.x!
The urlopen function is actually in the urllib2 module. Try import urllib2 and use urllib2.urlopen
I see that you are using Python2 or at least intend to use Python2.
urlopen helper function is available in both urllib and urllib2 in Python2.
What you need to do this, execute this script against the correct version of your python
C:\Python26\python.exe yourscript.py

Categories

Resources