Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Importing variables from another file
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?
Read the files and use the data in the program.
(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 and 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.
Importing doesn't seem to work.

How do I read the files and use the data in the program?
(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 and another called, and the contains the line
import player


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.
star imports like from player import * are bad practice and should be avoided.
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 One of the files is called

In main I write:


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.
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.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  importing CSV file into a HTML table using Python trybakov 1 143 Feb-22-2020, 09:47 PM
Last Post: scidam
  Importing a .py file with a value of a variable Sk4rd 2 343 Nov-09-2019, 08:29 AM
Last Post: buran
  Problem with importing a CSV file Chopan2211 6 1,050 Nov-06-2019, 09:25 AM
Last Post: DeaD_EyE
  variables from a text file vlk 2 263 Sep-22-2019, 06:44 PM
Last Post: vlk
  Importing/loading a library file (.so) tomasby 0 334 Aug-24-2019, 08:13 PM
Last Post: tomasby
  importing zip file on kaggle?? GuJu 4 840 Mar-10-2019, 02:21 PM
Last Post: buran
  I can't use file __init__ to store shared variables and classes in the package AlekseyPython 2 550 Feb-04-2019, 06:26 AM
Last Post: AlekseyPython
  Importing a .csv file to dataframe crispyduck 5 1,225 Jan-11-2019, 03:11 PM
Last Post: ichabod801
  Use Variables Generated from Functions in different files to use on the main file AykutRobotics 3 599 Jan-01-2019, 04:19 PM
Last Post: AykutRobotics
  variables help / update config file mapvis 3 677 Nov-27-2018, 02:24 PM
Last Post: mapvis

Forum Jump:

Users browsing this thread: 1 Guest(s)