Issue importing another class in the same directory - python

I'm trying to import a class Parser from a file parser.py in the same directory. However I keep getting the following error whenever I try to instantiate a Parser object:
File "bot.py", line 4, in <module>
from parser import Parser
ImportError: cannot import name Parser
I'm instantiating the object in the following way:
parser = Parser();
The same script works at my friend's setup so I'm not sure if it's a problem with the code. my version of python is 2.7.13. I'm including the imports below:
bot.py:
from parser import Parser
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
import csv
import os, sys, types
from random import randint
import urllib2
import json
import string
from bs4 import BeautifulSoup
import requests
parser.py:
import re
class Parser:
.....
##parser stufff

Did you try passing a relative path like
from .parser import Parser

Related

gremlin in AWS Glue with PySpark

I am trying to access gremlin via AWS Glue with PySpark as Runtime. As gremlinpython is external library, i had downloaded .whl file and placed in AWS S3. Now it was asking for "aenom" did the same. then isodate is required. So just wanted to know if there is anypackage which i can use instead of having separate modules.
Below is the sample script i am testing initially with all modules to keep it simple.
import boto3
import os
import sys
import site
import json
import pandas as pd
#from setuptools.command import easy_install
from importlib import reload
from io import StringIO
s3 = boto3.client('s3')
#dir_path = os.path.dirname(os.path.realpath(__file__))
#os.path.dirname(sys.modules['__main__'].__file__)
#install_path = os.environ['GLUE_INSTALLATION']
#easy_install.main( ["--install-dir", install_path, "gremlinpython"] )
#(site)
from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.process.traversal import T, Column
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
required libraries are below, after that there is no error related to modules.
tornado-6.0.4-cp35-cp35m-win32.whl
isodate-0.6.0-py2.py3-none-any.whl
aenum-2.2.4-py3-none-any.whl
gremlinpython-3.4.8-py2.py3-none-any.whl

What is the json file I need to read?

enter image description hereI need to download satellite images using python. I have found a code in GitHub but I did not understand what at this line. Please help me what it exactly is.
Visit https://github.com/kscottz/PythonFromSpace/blob/master/TheBasics.ipynb
import sys
import os
import json
import scipy
import urllib
import datetime
import urllib3
import rasterio
import subprocess
import numpy as np
import pandas as pd
import seaborn as sns
from osgeo import gdal
from planet import api
from planet.api import filters
from traitlets import link
import rasterio.tools.mask as rio_mask
from shapely.geometry import mapping, shape
from IPython.display import display, Image, HTML
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
urllib3.disable_warnings()
from ipyleaflet import (
Map,
Marker,
TileLayer, ImageOverlay,
Polyline, Polygon, Rectangle, Circle, CircleMarker,
GeoJSON,
DrawControl
)
%matplotlib inline
# will pick up api_key via environment variable PL_API_KEY
# but can be specified using `api_key` named argument
api_keys = json.load(open("apikeys.json",'r'))
client = api.ClientV1(api_key=api_keys["PLANET_API_KEY"])
# Make a slippy map to get GeoJSON
api_keys = json.load(open("apikeys.json",'r'))
client = api.ClientV1(api_key=api_keys["PLANET_API_KEY"])
What is the meaning of these two lines. What file should I upload for apikeys.json
You should follow this link to get an API Key.
https://support.planet.com/hc/en-us/articles/212318178-What-is-my-API-key-
apikeys.json is a JSON file of following format/content in json:
{"PLANET_API_KEY":"<Some API Key here>"}
json.load(...) API loads this json file as a dictionary

No attribute error in importing all module vs importing specific functions from module

When i wrote:
import urllib
fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
for line in fhand:
print(line.decode().strip())
The above function returns no attribute name 'request' found
but works when importing all the functions needed:
import urllib.request,urllib.parse,urllib.error
fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
for line in fhand:
print(line.decode().strip())
Any suggestions ?
From this answer: When you use an import statement it always searches the actual module path (and/or sys.modules); it doesn't make use of module objects in the local namespace that exist because of previous imports.
So if you want to use any object of the urllib, you need to type in the actual objects you want to use:
import urllib.request
You can also do that:
from urllib import request
fhand = request.urlopen('http://data.pr4e.org/romeo.txt')

Json, urlib2 and pprint

I have the following exercise:
Use the json module. First use urllib2 to download this file, then load the json as a python object and use pprint to make it look good when written to the terminal.
Now until now I've only worked with standard Python things (such as the codeacademy course and things such as lists).
What I understand is that I have to import urllib2 and apparently import json in some other way and use pprint...???
This is what I have done, but not sure if I got it right...
import urllib2
response = urllib2.urlopen('https://dl.dropboxusercontent.com/u/153071/test.json')
html = response.read()
import json
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(c) #Just printing a list from earlier in the file, not sure what to print...
You don't need to import pprint. You can specify indentation using the json module itself
import urllib2
import json
response = urllib2.urlopen('https://dl.dropboxusercontent.com/u/153071/test.json')
content_dict = json.loads(response.read())
print json.dumps(content_dict, indent=4)

Alternative way of connecting to PIL library?

Usually the library PIL is connected as follows:
from PIL import ImageTk, Image
I would like to connect it this way:
import PIL
but my version does not work. Here's the code:
import os, sys
import tkinter
import PIL
main = tkinter.Tk()
catalogImg1 = 'imgs'
nameImg1 = 'n.jpg'
pathImg1 = os.path.join(catalogImg1, nameImg1)
openImg = PIL.Image.open(pathImg1)
renderImg = PIL.ImageTk.PhotoImage(openImg)
tkinter.Label(main, image=renderImg).pack()
main.mainloop()
The error message is:
Traceback (most recent call last): File
"C:\Python33\projects\PIL_IMAGETK\ImageTK_photoimage - копия.py", line
11, in
openImg = PIL.Image.open(pathImg1) AttributeError: 'module' object has no attribute 'Image'
Importing a package (PIL) does not automatically import subpackages, submodules (PIL.Image, PIL.ImageTk). (Unless the package itself do it).
Explicitly import the submodules.
Replace following line:
import PIL
with:
import PIL.Image
import PIL.ImageTk
This is because, Image is a submodule within the PIL package i.e. It is not a function or class. Importing a package does not automatically import its submodules.
If you want to use the PIL namespace, you can import the module as follows:
import PIL.Image
openImg = PIL.Image.open(pathImg1)
If you want to import all the submodules of PIL, you can do the following
from PIL import *
openImg = Image.open(pathImg1)

Categories