I have a problem with os module in python - python

I've recently had a problem with (i think) the os module in python:
Traceback (most recent call last):
File "main.py", line 9, in <module>
api = getApi(os.environ['consumer_key'], os.environ['consumer_secret'], os.environ['access_token_key'], os.environ['access_token_secret'])
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\os.py", line 681, in __getitem__
raise KeyError(key) from None
KeyError: 'consumer_key'
my code of main.py is:
from config import getApi
import os
import sys
import time
print()
api = getApi(os.environ['consumer_key'], os.environ['consumer_secret'], os.environ['access_token_key'], os.environ['access_token_secret'])
my code of config.py is
import twitter
import os
def getApi(consumer_key, consumer_secret, access_token_key, access_token_secret):
return twitter.Api(consumer_key='*********',
consumer_secret='*********',
access_token_key='*********',
access_token_secret='*********')
Send tweets with the postUpdate is possible if I write the keys in the main.py but when I put the keys in the config.py, it don't works
Can anyone help me please ?

It's not an error with the os module, the keys simply aren't in the enviroment. If you're using a .env file you should use a module like dotenv to load the file.

Related

KeyError: 'type' Clarifai

I'm starting on a project using Clarifai. However, when I define the app, I'm getting a key error:
from clarifai.rest import ClarifaiApp
from clarifai.rest import Image as ClImage
import os
from glob import glob
api_key = 'my api key'
app = ClarifaiApp(api_key=api_key) # Error occurs here
model_id = 'model id'
concepts = ['concept1', 'concept2', 'concept3']
Traceback (most recent call last):
File "C:\Users\crayo\uShoe\main.py", line 6, in <module>
app = ClarifaiApp(api_key=api_key)
File "C:\Users\user\project\venv\lib\site-packages\clarifai\rest\client.py", line 124, in __init__
self.models = Models(self.api, self.solutions) # type: Models
File "C:\Users\user\project\venv\lib\site-packages\clarifai\rest\client.py", line 1068, in __init__
self.model_id_cache = self.init_model_cache()
File "C:\Users\user\project\venv\lib\site-packages\clarifai\rest\client.py", line 1088, in init_model_cache
model_type = m.output_info['type']
KeyError: 'type'
I'm not sure what's causing this error, so if someone could provide input I'd appreciate it! Thanks!
You are using the deprecated Python REST package: https://github.com/Clarifai/clarifai-python. Please replace your code with the new & updated Python gRPC client: https://github.com/Clarifai/clarifai-python-grpc
Make sure to uninstall the REST package to avoid conflicts.
You can find our API docs & code snippets here: https://docs.clarifai.com

ValueError: source code string cannot contain null bytes - Error after scipy.io import

After updating scipy, numpy and pandas to the newest versions, I receive the following error whenever I attempt to run my code on a Windows 10 machine with Python 3.7.4:
Traceback (most recent call last):
...
File "Path\To\MyClass.py", line 3, in <module>
import scipy.io as sio
File "Path\To\Anaconda\lib\site-packages\scipy\__init__.py", line 68, in <module>
from ._lib.deprecation import _deprecated
File "Path\To\Anaconda\lib\site-packages\scipy\_lib\__init__.py", line 12, in <module>
from scipy._lib._testutils import PytestTester
ValueError: source code string cannot contain null bytes
This is how the last file looks like:
"""
Module containing private utility functions
===========================================
The ``scipy._lib`` namespace is empty (for now). Tests for all
utilities in submodules of ``_lib`` can be run with::
from scipy import _lib
_lib.test()
"""
from scipy._lib._testutils import PytestTester
test = PytestTester(__name__)
del PytestTester
Am I running into a bug or is my setup broken?
I managed to solve this issue by reinstalling Anaconda. I still don't know the source of the problem though.

Google App Engine httplib2 import Http results in Syntax Error

Looking for some help with Google App Engine. Below is the offending part of my code:
from __future__ import print_function
import webapp2
import jinja2
import csv
import os
from collections import namedtuple
from httplib2 import Http
from googleapiclient.discovery import build
from oauth2client import file, client, tools
import datetime
##code to do stuff
Where I'm running into trouble is with the "from httplib2 import Http" line. I'm getting the following error:
Traceback (most recent call last):
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/c3b7fd7c606f3aa7/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/c3b7fd7c606f3aa7/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/c3b7fd7c606f3aa7/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/base/data/home/apps/k~fraseline2019/20181014t160622.413269158822473824/main.py", line 10, in <module>
from httplib2 import Http
File "/base/data/home/apps/k~fraseline2019/20181014t160622.413269158822473824/lib/httplib2/__init__.py", line 382
print('%s:' % h, end=' ', file=self._fp)
^
SyntaxError: invalid syntax
I've got the httplib2 module files in my app directory. I've been googling around but nobody seems to have had this issue - can someone help?
THANK YOU!
Please read about MCVEs. It would appear that the MCVE for this issue is one line:
from httplib2 import Http
I believe your issue is that your httplib2 module lacks the needed future import.
from __future__ import print_function
Future imports only apply to the module in which they appear. Without it, in 2.7, one gets the traceback you got.
>>> print(1, 2, end='')
File "<stdin>", line 1
print(1, 2, end='')
^
SyntaxError: invalid syntax

No module named 'x'; 'x' is not a package

This is the error that appears when I try to import other tkinter files with the use of a button:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Feargus\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\Users\Feargus\Documents\COMPUTING\games\BIG_MAIN_MENU.py", line 13, in settingsButton
import SETTINGS.py
ModuleNotFoundError: No module named 'SETTINGS.py'; 'SETTINGS' is not a package
Here is the code which imports the files:
def startGame():
import GAME.py
def settingsButton():
import SETTINGS.py
quit()
def leaderBoard():
import Leaderboard.py
quit()
def endGame():
quit()
The problem is that you're adding the .py extensions for all your import statements. You just need the file names when importing Python modules so
import SETTINGS instead of import SETTINGS.py
would be the correct way to format it.
The same goes to the other imports as well.

cannot import name SharePointSite

I've got the same error message both on my desktop and on my Windows 2008 R2 server -
Here's the code -
from sharepoint import SharePointSite, basic_auth_opener
server_url = "http://sharepoint/"
site_url = server_url + "path/to/page/Forms/AllItems.aspx"
opener = basic_auth_opener(server_url, "acct", "password")
site = SharePointSite(site_url, opener)
for sp_list in site.lists:
print sp_list.id, sp_list.meta['Title']
When running it I get the following error -
Traceback (most recent call last):
File "C:\temp\sharepoint.py", line 1, in <module>
from sharepoint import SharePointSite, basic_auth_opener
File "C:\temp\sharepoint.py", line 1, in <module>
from sharepoint import SharePointSite, basic_auth_opener
ImportError: cannot import name SharePointSite
What's going on? The package is in the location -
C:\Python27\Lib\site-packages\sharepoint
I can import other packages just fine. for example, lxml works fine.
from lxml import etree
no problems.
You named your script sharepoint.py and that masks the library:
Traceback (most recent call last):
File "C:\temp\sharepoint.py", line 1, in <module>
from sharepoint import SharePointSite, basic_auth_opener
File "C:\temp\sharepoint.py", line 1, in <module>
from sharepoint import SharePointSite, basic_auth_opener
ImportError: cannot import name SharePointSite
Look at the filenames in the traceback, you can see that the script ends up importing itself; when Python starts your script it loads it as __main__, so importing sharepoint loads your own file one more time, at which point it fails to import itself again.
Rename your script to something else.

Categories

Resources