AttributeError in Python when creating FileCookieJar (cookielib Py 2.6) - python

I'm trying to create a new FileCookieJar using cookielib in Python 2.6. However, when I do so I get the following error:
Traceback (most recent call last):
File "C:\Users\Paul\getfile.py", line 7, in <module>
cj.load(os.getcwd()+'\cookies.txt')
File "C:\Python26\lib\cookielib.py", line 1763, in load
self._really_load(f, filename, ignore_discard, ignore_expires)
AttributeError: FileCookieJar instance has no attribute '_really_load'
My code is as follows:
import cookielib, urllib2, os
cj = cookielib.FileCookieJar()
cj.load(os.getcwd()+'\cookies.txt')
I've not used Python in years but need to for a small project. Can anyone help?

FileCookieJar is a kind of abstract class (i.e. it has some functionality unimplemented). You probably want to use one of its children - MozillaCookieJar, MSIECookieJar or LWPCookieJar.

Related

pypub module for python

Getting this error while using epub python package or you can say epub library for python , wondering what to do about it. please help..
There are my codes:
import pypub
my_first_epub = pypub.Epub('My first Epub')
my_first_chapter = pypub.create_chapter_from_url('https://en.wikipedia.org/wiki/EPUB')
my_first_epub.add_chapter(my_first_chapter)
my_first_epub.create_epub('OUTPUT_DIRECTORY')
There are the errors:
runcell(0, 'E:/python练习/untitled0.py')
Traceback (most recent call last):
File E:\python练习\untitled0.py:8 in <module>
import pypub
File D:\anaconda3\lib\site-packages\pypub\__init__.py:2 in <module>
from epub import Epub
ImportError: cannot import name 'Epub' from 'epub' (D:\anaconda3\lib\site-packages\epub\__init__.py)
Pypub not available at python 3.x, only in python 2.x. Read docs (first point at tutorial): https://pypub.readthedocs.io/en/latest/tutorial_reddit.html

AttributeError: module 'flask.views' has no attribute 'MethodViewType'

Since two days ago I receive this the moment I try to import flask_apispec.extension:
>>> import flask_apispec.extension
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\MyUser\AppData\Local\pypoetry\Cache\virtualenvs\gfp-LphObOQn-py3.9\lib\site-packages\flask_apispec\__init__.py", line 1, in <module>
from flask_apispec.views import ResourceMeta, MethodResource
File "C:\Users\MyUser\AppData\Local\pypoetry\Cache\virtualenvs\gfp-LphObOQn-py3.9\lib\site-packages\flask_apispec\views.py", line 41, in <module>
class MethodResourceMeta(ResourceMeta, flask.views.MethodViewType):
AttributeError: module 'flask.views' has no attribute 'MethodViewType'
I'm attempting to import it after apispec and marshmallow (libraries were installed using poetry):
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
import flask_apispec.extension
I have the following versions:
apispec = "^5.2.2"
Flask = "2.1.1"
flask_apispec = "0.11.1"
Different versions didn't help, resetting the environment didn't help either...
Flask has removed MethodViewType from flask.views and flask-apispec still uses it.
There is a PR for flask-apispec that may fix it, but as writing this answer, it is not merged into the main branch. You may try it by running
pip install git+https://github.com/jmcarp/flask-apispec.git#refs/pull/238/merge
More info on MethodViewType removal
More info on the fix

Unable to import one python file into another [duplicate]

is there a way to import multiple python files into a main python file?
I have a bunch of py files and each one has to run in the main python file and the data are saved into a json file.
This is what I tried and it gave me an error.
import light.py as light
Error:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/Desktop/majorproject/pillar1.py", line 8, in <module>
import sensorkey.py as sensorkey
ImportError: No module named 'sensorkey.py'; 'sensorkey' is not a package
I have also tried specifying the path to the py file and it didn't work either and keeps giving an invalid syntax error.
import /home/pi/Desktop/json/light.py as light
Update:
I managed to fix the import error but i now, after importing this light.py file, i have to print out certain keys from a dictionary (key) into this new file then export it to a json file. I'm currently using TinyDB to do so. Here are my codes:
from tinydb import TinyDB, Query
import json
from light import key
with open("/home/pi/Desktop/json/sensortestest.json", 'w+'):
db = TinyDB('/home/pi/Desktop/json/sensortestest.json')
table = db.table('Light')
db.insert_multiple([{'Key 1' :key[lightkey]}, {'Key 2' : key[lightkeyID]}])
Error:
Traceback (most recent call last):
File "/home/pi/Desktop/majorproject/testertestest.py", line 12, in <module>
db.insert_multiple([{'Key 1' :key[lightkey]}, {'Key 2' : key[lightkeyID]}])
NameError: name 'lightkey' is not defined
The problem is I had already defined 'lightkey' in its own file already.
To include the dictionary, you could do this if your file location is in different directory (with caution of path.append as #Coldspeed mentioned):
import sys
sys.path.append("path/foo/bar/")
from light import *
If it is in same directory as current directory, you could just do:
from light import *
The syntax for importing your_filename.py, assuming it is in the same directory, is
import your_filename
In your case, it would be
import light
Note the absence of .py.
If your file is in a different directory, you'll need to do:
import sys
sys.path.append('path/to/dir/containing/your_filename.py')
import your_filename
Note that appending to sys.path is dangerous, and should not be done unless you know what you're doing.
Read more at the official docs for import.

Unable to use CsvFileSource from beam_utils.sources in apache beam program on Cloud Dataflow

Unable to import CsvFileSource but able to install beam_utils.
I need this import to run the Cloud data flow program.
Code has -
from beam_utils.sources import CsvFileSource
Error message :
>>> from beam_utils.sources import CsvFileSource
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/vk/.local/lib/python2.7/site-packages/beam_utils/sources.py", line 14, in <module>
class JsonLinesFileSource(beam.io.filebasedsource.FileBasedSource):
File "/home/vk/.local/lib/python2.7/site-packages/beam_utils/sources.py", line 17, in JsonLinesFileSource
compression_type=fileio.CompressionTypes.AUTO,
AttributeError: 'module' object has no attribute 'CompressionTypes'
>>>
I even tried import using python3.
any idea, how I can bypass.
Apache beam has recently updated and changed a few of their methods and attributes.
In particular, the fileio class is now filesystem. If you want to quickfix this, you can edit beam_utils source (python_home\lib\site-packages\beam_utils\sources.py) and replace 'fileio' for 'filesystem'. It should work ;)
If you take a look at the github repo (https://github.com/pabloem/beam_utils/blob/master/beam_utils/sources.py), the changes are already there. I guess it's a matter of time until they're added to pip!
I'm also getting the same error. I'm attemping to load csv into a dictionary and then write it to local (eventually to BQ).
argv = [
'--project={0}'.format(PROJECT),
'--staging_location=gs://{0}/'.format(BUCKET),
'--temp_location=gs://{0}/'.format(BUCKET),
#'--runner=DataflowRunner'
'--runner=DirectRunner'
]
p= beam.Pipeline(argv=argv)
rows = p | 'ReadCSV' >> beam.io.Read(CsvFileSource('gs://blahblah/file.csv')) | 'Write to file' >> beam.io.WriteToText('s
trings', file_name_suffix='.txt')```
-[snip]-
Apache Beam will soon support Python 3 only.
'You are using Apache Beam with Python 2. '
Traceback (most recent call last):
File "avg-ecom-rating.py", line 5, in <module>
from beam_utils.sources import CsvFileSource
File "/home/dlemon/env/local/lib/python2.7/site-packages/beam_utils/sources.py", line 14, in <module>
class JsonLinesFileSource(beam.io.filebasedsource.FileBasedSource):
File "/home/dlemon/env/local/lib/python2.7/site-packages/beam_utils/sources.py", line 17, in JsonLinesFileSource
compression_type=fileio.CompressionTypes.AUTO,
AttributeError: 'module' object has no attribute 'CompressionTypes'

What kind of object do I need to use the options parameter of fix_multiple_files?

I need to create a simple script for fixing indentation in python scripts of the repository. I found fix_multiple_files in autopep8 module:
import autopep8
autopep8.fix_multiple_files('/home/playrix/work/ci-teamcity/scripts/', options={'recursive': 1})
I'm getting this every time I run the script:
Traceback (most recent call last):
File "/home/playrix/work/ci-teamcity/scripts/1.py", line 4, in <module>
autopep8.fix_multiple_files('/home/playrix/work/ci-teamcity/scripts/', options='')
File "/home/playrix/.local/lib/python2.7/site-packages/autopep8.py", line 3897, in fix_multiple_files
filenames = find_files(filenames, options.recursive, options.exclude)
AttributeError: 'str' object has no attribute 'recursive'
How correctly noticed johnsharpe it was argparse.Namespace. And the best way to get it was:
opt = autopep8.parse_args(arguments=['recursive', '--in-place', '--aggressive'])

Categories