-1

I have two Python files. In my main file I work with a openpyxl module. In my second file I have many string lines with concatenating using Excel file cells, for example:

'/ip address=' + sheet['D'+ row].value + '\n'

and many others. But there is a problem, if I import that file to a main file using:

from file2 import *

I get many errors about undefined names like:

NameError: name 'sheet' is not defined

And it is really defined only in my main file, like:

wb = openpyxl.load_workbook(filename='clients.xlsx')
sheet = wb.get_sheet_by_name('Page1')

How can I import everything from my file2 and get it work?

dtsn
  • 1
  • why not import openpyxl into file 2? – Veselin Davidov May 10 '18 at 13:51
  • Well, I should then define many other large string variables in my file2, cause it also has many refers to them. And it adds too many lines to a file2 + i want to have some more files in future – dtsn May 10 '18 at 13:56
  • well but otherwise it doesn't compile properly. I think python doesn't just add the files into one big file and then compile. It is compiling on the fly and your imported file is not compiling properly – Veselin Davidov May 10 '18 at 14:07

1 Answers1

-1

As far as I can wrap my head around it, import only imports functions. execfile(*path*) should work for you in your case.
There are some more ways to import python into python, which you might want to check out.

mozi_h
  • 41
  • 6
  • No, `import` imports everything. But everything must be qualified with the name of the module: if you `import file`, then you access stuff at the top level of `file.py` as `file.wb` or `file.sheet`. – Andrew Jaffe May 11 '18 at 09:41