Global name 'number' is not defined on module import - python-2.7

I'm using python 2.7 on linux machine and I'm sure this is an obvious question but
I really need this answer.
Module one named one in file test
def one():
number=1
Module two named two in file exam
def two():
if number == 1:
print "apples and oranges"
and both of them imported into a container named modi.py like this
import test, exam
test.one()
exam.two()
I was hoping to set the variable "number" in module one and reference it in module two but I keep getting the name error "global name 'number' is not defined"
I just can't see the problem

This is because number is in test.py. Try adding global number before the first use in test.py and import test in exam.py. Then use if test.number == 1:

Related

Python 'rawpy._rawpy.RawPy' object has no attribute 'imread' after second pass

I try to process a series of DNG raw picture files and it all works well for the first pass (first fils). When I try to read the second DNG file during the second pass through the for-next loop, I receive the error message 'rawpy._rawpy.RawPy' object has no attribute 'imread' when executing the line "with raw.imread(file) as raw:".
import numpy as np
import rawpy as raw
import pyexiv2
from scipy import stats
for file in list:
metadata = pyexiv2.ImageMetadata(file)
metadata.read()
with raw.imread(file) as raw:
rgb16 = raw.postprocess(gamma=(1,1), no_auto_bright=True, output_bps=16)
avgR=stats.describe(np.ravel(rgb16[:,:,0]))[2]
avgG=stats.describe(np.ravel(rgb16[:,:,1]))[2]
avgB=stats.describe(np.ravel(rgb16[:,:,2]))[2]
print i,file,'T=', metadata['Exif.PentaxDng.Temperature'].raw_value,'C',avgR,avgG,avgB
i+=1
I tried already to close the raw object but from googling I understand that is not necessary when a context manager is used.
Help or suggestions are very welcome.
Thanks in advance.
You're overwriting your alias of the rawpy module (raw) with the image you're reading. That means you'll get an error on the second pass through the loop.
import rawpy as raw # here's the first thing named "raw"
#...
for file in list:
#...
with raw.imread(file) as raw: # here's the second
#...
Pick a different name for one of the variables and your code should work.

how to Looping python scripts together

I have two files. An 'initialization' script aka file1.py and a 'main code' script aka 'main_code.py'. The main_code.py is really a several hundred line .ipynb that was converted to a .py file. I want to run the same skeleton of the code with the only adjustment being to pass in the different parameters found in the 'file1.py' script.
In reality, it is much more complex than what I have laid out below with more references to other locations / DBs and what not.
However, I receive errors such as 'each_item[0]' is not defined. I can't seem to be able to pass in the values/variables that come from my loop in file1.py to my script that is contained inside the loop.
Must be doing something very obviously wrong as I imagine this is a simple fix
file1.py:
import pandas as pd
import os
import bumpy as np
import jaydebeapi as jd
#etc...
cities = ['NYC','LA','DC','MIA'] # really comes from a query/column
value_list = [5,500,5000,300] # comes from a query/column
zipped = list(zip(cities,value_list)) # make tuples
for each_item in zipped:
os.system('python loop_file.py')
# where I'm getting errors.
main_code.py:
names = each_item[0]
value = each_item[1]
# lots of things going on here in real code but for simplicity...
print value = value * 4
print value

Understanding try and except with __builtin__

New to Python and have yet to use or come across __builtin__ and was curious about what the following code is actually doing:
import sys
try:
import __builtin__
input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
pass
username = input("username: ")
password = input("password: ")
Is this code being used to basically check whether or not the script is being run with Python 2 or Python 3 and if it's Python 2, input() is converted to raw_input()?
This is rather poorly-written code to make input() refer to raw_input() if the latter exists. That makes it compatible with both Python 2 and Python 3. A simpler way to do it would be this:
try:
input = raw_input
except NameError:
pass
username = input("username: ")
password = input("password: ")
The __builtin__ module is the module where all the builtin objects like input() and raw_input() live. But we don't need it in this case. In Python 3.x, this is called builtins, which is why the author of this code is catching ImportError.
If you need to do this kind of thing in general, it's a good idea to use six rather than coding all these things by hand.

how to assign a value to global variable in runtime in python

While running a code in python I want to update a dictionary value that is in config(global variable) I'm trying to do but i got the following error.
config.py
survey={'url':''}
runnigcode.py
url=config.__getattribute__(config.environment)
url['url']='myurl.com'
TypeError: "'module' object does not support item assignment"
in your runningcode.py, just import the variable as a method:
from config import survey
print survey
if you want to modify the value in the config.py, just use write method, like in this post: Edit configuration file through python

Import a list from a different file

Is there a way I can import a list from a different Python file? For example if I have a list:
list1 = ['horses', 'sheep', 'cows', 'chickens', 'dog']
Can I import this list into other files? I know to import other functions you do
from FileName import DefName
This is a user defined list and I don't want to have the user input the same list a million times.
Just a few maybes as to how this could be done:
from FileName import ListName or put all the lists into a function and then import the definition name
Thanks for the help
One option is to dump that list into a temp file, and read it from your other python script.
Another option (if one python script calls the other), is to pass the list as an argument (e.g. using sys.argv[1] and *args, etc).
I'll just export the lists in a file. Therefore every piece of code can read it.