So I've recently been working on a simple project, and I've used "Jaheim Archibald's Login System" for logging in to my thing. I've found a variable that is the username entered, and I want to share that variable across another script that I'm creating. I've tried using the 'config' module, but it just gives me an attribute error.
EDIT:
Here is the sharing I tried to do:
Loginregister.py:
import config
config.x = userName
Account.py:
import config
print(config.x)
Error:
Traceback (most recent call last):
File "Account.py", line 3, in <module>
print(config.x)
AttributeError: module 'config' has no attribute 'x'
Related
Trying to call a function yamlCreation() which is in another directory into this python file ..
the path of this file is
path:bss_micro_svcs/bss_micro_svcs/kubectl.py
Calling File: "kubectl.py"
from bss_micro_svcs.kubectl_utils.templating_to_yaml import yamlCreation
class Kube(Svcs):
def startService(self, cluster_info, service, service_profile):
yamlCreation()
k=Kube()
k.startService()
yamlCreation() function is in this python file named "templating_to_yaml.py"
the path of the file is..
path:bss_micro_svcs/kubectl_utils/templating_to_yaml.py
function to be called is inside this file:"templating_to_yaml.py"
file_loader = FileSystemLoader('bss_micro_svcs/bss_micro_svcs/yamls/templates')
env = Environment(loader=file_loader)
#deployment.yaml templating
deployment_template=env.get_template('deployment.tmpl')
deployment_output=deployment_template.render(deployment_name='nginx-deployment',deployment_app='nginx',deployment_replicas='3',deployment_container_name='nginx',deployment_image='nginx:1.14.2',deployment_container_port=80)
deployment_outfile=open('bss_micro_svcs/bss_micro_svcs/yamls/services/integration/deployment.yaml','w')
deployment_outfile.write(deployment_output)
deployment_outfile.close()
#*********************************************************************************************************************************************************#
#service.yaml templating
service_template=env.get_template('services.tmpl')
service_output=service_template.render(service_name='integration-service',service_name_space='demo-sandbox',service_app='integration',service_port=8090,target_port=8090)
service_outfile=open('bss_micro_svcs/bss_micro_svcs/yamls/services/integration/services.yaml','w')
service_outfile.write(service_output)
service_outfile.close()
GETTING AN ERROR:
┌──(bss_micro_svcs-jnO67Li7)(RK㉿kali)-[~/Desktop/servicelaunchmgr]
└─$ /home/RK/.local/share/virtualenvs/bss_micro_svcs-jnO67Li7/bin/python /home/RK/Desktop/servicelaunchmgr/bss_micro_svcs/bss_micro_svcs/kubectl.py
Traceback (most recent call last):
File "/home/RK/Desktop/servicelaunchmgr/bss_micro_svcs/bss_micro_svcs/kubectl.py", line 2, in <module>
from bss_micro_svcs.kubectl_utils.templating_to_yaml import yamlCreation
ModuleNotFoundError: No module named 'bss_micro_svcs'
You could set environment variable PYTHONPATH so the interpreter could know where to start looking for the module to import. I would suggest to set it in the root module.
Reference: https://bic-berkeley.github.io/psych-214-fall-2016/using_pythonpath.html
I'm trying to import the requests module to get familiar with bs4, but the request module in the file I'm currently working in is grayed out so it isn't being recognized as a module. When I run the almost empty program, I get an error for an unrelated python file within my project.
Should I individually store each python file I make inside of a separate folder?
Both of these files are inside of the same project folder.
import requests
response = get('https://www.newegg.ca/p/N82E16868105274')
print(response.raise_for_status())
Error:
Traceback (most recent call last):
File "C:\Users\Denze\MyPythonScripts\Webscraping learning\beautifulsoup tests.py", line 1, in <module>
import requests
File "C:\Users\Denze\MyPythonScripts\requests.py", line 3, in <module>
res = requests.get('')
AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)
Process finished with exit code 1
The other code in question that I think is causing my error:
import requests
res = requests.get('')
playFile = ('TestDownload.txt', 'wb')
for chunk in res.iter_content(100000):
playFile.write(chunk)
playFile.close()
You have a name collision. You're not importing the requests library, you're importing your script.
You wanted to do the following with your imports:
MyPythonScripts\beautifulsoup tests.py
→ requests.get() (the library)
What you're doing instead is:
MyPythonScripts\beautifulsoup tests.py
→ MyPythonScripts\requests.py
→ MyPythonScripts\requests.py .get() (the same file again)
That's the "circular import" that is mentioned in the traceback. The module imports itself and tries to use an attribute that isn't there before it finishes "executing", so the interpreter thinks it's due to the unfinished initialization
Raname MyPythonScripts\requests.py to something else and it should work.
Requirement:
Commit and push files to GitHub repository from a python script.
The credentials should be included in the script.
Issue:
If credentials are provided in the script the commit operation is
executing and throwing the following error,
Traceback (most recent call last):
File "/home/amith/example.py", line 14, in <module>
repo.index.add(folder_path)
AttributeError: 'Repository' object has no attribute 'index'
If credentials are, not provided in the script the commit operation is working properly by providing it on the terminal.
I need to integrate this script in Django application which should accept the credentials from the configuration file.
I have tried the following links but nothing has worked for me yet.
- link1
- link2
- link3
from git import Repo
from github import Github
from pdb import set_trace as bp
repo_dir = '--------'
repo = Repo(repo_dir)
# using username and password
g = Github("-----", "------")
folder_path = '----------'
commit_message = 'Add New file'
repo.index.add(folder_path)
repo.index.commit(commit_message)
origin = repo.remote('origin')
origin.push()
So, I am getting this error "AttributeError: 'Repository' object has no attribute 'index'".
Complete error -
Traceback (most recent call last):
File "/home/amith/example.py", line 14, in <module>
repo.index.add(folder_path)
AttributeError: 'Repository' object has no attribute 'index'
I am learning python and I am trying to do a simple task of reading information from a config file.
So using the Python Doc and this similar problem as a reference I created two files.
This is my config file config.ini (also tried config.cfg)
[DEFAULT]
OutDir = path_to_file/
[AUTH]
TestInt = 100
TestStr = blue
TestParse = blua
and this is my python file test.py
import ConfigParser
from ConfigParser import *
config = ConfigParser()
config.read(config.cfg)
for name in config.options('AUTH'):
print name
out = config.get('DEFAULT', 'OutDir')
print 'Output directory is ' + out
however when running the command python test.py I am erroring out and receiving this error
Traceback (most recent call last):
File "test.py", line 7, in <module>
config.read(config.cfg)
AttributeError: ConfigParser instance has no attribute 'cfg'
Note: I thought that meant it the extension couldn't be read so I created the .ini file and changed it in the code and I received the same error but it instead read ...has no attribute 'ini'
I am not sure what I am doing wrong since I am doing the exact same as the python doc and the solution someone used to fix this similar issue.
config.read takes a string as its argument. You forgot to quote the file name, and config was coincidentally the name of a Python object (the module) that potentially could have a cfg attribute. You'd get an entirely different error if you had written config.read(foobarbaz.ini).
The correct line is
config.read('config.cfg') # or 'config.ini', if that's the file name
Summary:
The same import statement that works when __name__ == "__main__" stops working when imported from another module outside the immediate package. If I fix it so that it works when imported, it stops working when __name__ == "__main__". Is there a win-win solution for these conflicting scopes?
In more detail:
Say I have the following system layout.
package/
__init__.py
outer_module.py
subpackage/
__init__.py
inner_module_1.py
inner_module_2.py
And say that all modules are empty except for the following two.
# inner_module_1
import inner_module_2
&
# outer_module
import subpackage.inner_module_1
import subpackage.inner_module_2
If I run inner_module_1, there is no error.
If I run outer_module, I get the following ImportError from inner_module_1.
Traceback (most recent call last):
File "C:\package\outer_module.py", line 1, in <module>
import subpackage.inner_module_1
File "C:\package\subpackage\inner_module_1.py", line 1, in <module>
import inner_module_2
ImportError: No module named 'inner_module_2'
I can prevent that error by adding the package name to the import statement in inner_module_1 as follows.
# inner_module_1
import subpackage.inner_module_2
Although this change allows outer_module to run without an error, now running inner_module_1, which had before run without error, raises the following error.
Traceback (most recent call last):
File "C:\package\subpackage\inner_module_1.py", line 1, in <module>
import subpackage.inner_module_2
ImportError: No module named 'subpackage'
So here are my questions.
Is the import statement in inner_module_1 interpreted differently when accessed by outer_module than when run in inner_module_1, and if so, how so?
Can I write an import statement in inner_module_1 that works both when inner_module_1 is run and also when outer_module is run, and if so, what is that statement?
Thanks,
Victor