-2

I need some assitance since I really have no idea how I can fix this:

x="test"

y="test2"

When I try to import y from x , it says that there is no file with the name "x" (from x import y)

Is there any way to import test2 from test

where test2 is a variable inside the filename test

(The code I'm currently using)

start="trans" 

x= ["a","b","c","d","e","f","g","h","i","j"]

while len(x) !=1:

    global begin

    del x[0]

    print(x[0])

    list_to_string= ''.join(x)

    start="start"+list_to_string[0]

    print(start)

else:

    print("stop")


from start import start

Thanks already :)

lvc
  • 34,233
  • 10
  • 73
  • 98
Leyundai
  • 3
  • 1
  • where is `test2` ?...and where is `test`? and what is `start`?..really lost here! – Iron Fist Jun 23 '15 at 07:56
  • Or you mean to import `start` variable?..but from where? – Iron Fist Jun 23 '15 at 07:58
  • possible duplicate of [import module from string variable](http://stackoverflow.com/questions/8718885/import-module-from-string-variable) – Holt Jun 23 '15 at 07:59
  • @Khalil test is a .py file and test2 is list inside a .py file , I want to import test2 from the py file test in a new file but instead of importing like : from test import test2 I want from x import y where X = "test" and y = "test2" – Leyundai Jun 23 '15 at 08:04
  • I see no `test2` variable in code – Andersson Jun 23 '15 at 08:08

1 Answers1

0

You can't use a variable for an import statement.

from x import y will try to find a module literally called y. In your case, that probably means you want a file in the same directory called y.py. It will then try to find a variable (including, eg, a function or class name) in that module which is literally called x - ie, you need to do something in that file like:

x = 5

or:

def x():
    return 5

If you really want to use a string variable here, then there are ways to do that (see, eg, import module from string variable), but you probably don't need to do that here. It looks like you just want:

from test import test2
Community
  • 1
  • 1
lvc
  • 34,233
  • 10
  • 73
  • 98