Python Forum
Importing variables from another file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Importing variables from another file (/thread-23800.html)



Importing variables from another file - IILawrenceII - Jan-17-2020

I've been googling this problem for a while now and nothing seems to work.

Here's my problem:

I have variables in 4 different files. I want these variables to be read by my main file. I started programming today, so I thought a simple text based combat sim would be something nice to start off with.

In one file I put variables for my character. In another I put variables for an enemy. And so on.

In the main file I would like to call all these variables into play to make the fight calculations.

So, what exactly do I have to type in to get these other files into play?


RE: Importing variables from another file - woooee - Jan-17-2020

Read the files and use the data in the program.


RE: Importing variables from another file - michael1789 - Jan-17-2020

(Jan-17-2020, 08:32 PM)IILawrenceII Wrote: I've been googling this problem for a while now and nothing seems to work.

What you are looking to google is to "import modules".

If you save your file as eg player.py and enemy.py at the top of your main file put
import player
import enemy
to read it in. You can put any python code into your modules, like functions and classes too. Not just variable.


RE: Importing variables from another file - IILawrenceII - Jan-17-2020

Importing doesn't seem to work.

How do I read the files and use the data in the program?


RE: Importing variables from another file - michael1789 - Jan-18-2020

(Jan-17-2020, 09:45 PM)IILawrenceII Wrote: Importing doesn't seem to work.

How do I read the files and use the data in the program?

...You import it. That's the exact answer to your question. Are you following a tutorial for your game? How are you trying to learn Python?

How does it not work? Do you get an error? The files need to be .py and they need to be in the same folder. If you have one file called main.py and another called player.py, and the main.py contains the line
import player

or

from player import *
Everything below the import statement can see it just as if it were in the same file. Importing it isn't going to run anything, if that is what you were wanting.

If that doesn't work then something is wrong with the code you are trying to run and you'll have to post it here to see.


RE: Importing variables from another file - buran - Jan-18-2020

star imports like from player import * are bad practice and should be avoided.


RE: Importing variables from another file - IILawrenceII - Jan-18-2020

It says module doesn't exist, though I have checked the names etc and everything is exactly the way you have suggested. I don't understand what is not working.

All files are in the same folder. They're all .py files. My main file is called main.py. One of the files is called playerData.py.

In main I write:

import playerData.py

and I tried

import playerData

It says module doesn't exist. It says this on all the files.

The files contain only variables and nothing else. Also, the only command at this point I have used is print() just to see if it writes out what I am wanting to do. I am not following a tutorial, since I could not find a tutorial on this topic of importing. I did see importing in a guide somewhere, but since it didn't work, I looked for more tutorials, and in one guide it said that the import command no longer works for other files. I tried searching what does but could not find anything.

I am using the newest python, which I would think is obvious but now after this I'm not sure.

Module isn't found, and that's where it stops. I also tried to do this importing in the online python web page and it didn't work.


RE: Importing variables from another file - IILawrenceII - Jan-18-2020

I redid all the file names and saved them into a new folder and that seemed to work.

Now it would seem my text based combat test actually works. Now I have to create random encounters and make the script more fluid instead of setting all the stats myself. Thanks guys! Not sure what went wrong but refreshing everything sometimes works :)

Also, for future reference, it seems one cannot import a whole module into a module that isn't the main one I guess? I had to import all things separately to the other files.