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

How do I read the files and use the data in the program?
Reply
#5
(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.
Reply
#6
star imports like from player import * are bad practice and should be avoided.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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.
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 245 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  File path by adding various variables Mishal0488 2 964 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 698 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  Problem with importing Python file in Visual Studio Code DXav 7 4,910 Jun-15-2022, 12:54 PM
Last Post: snippsat
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,329 May-17-2022, 07:49 AM
Last Post: Pedroski55
  Storing variables into one file for use in multiple Jupyter notebooks devansing 1 1,695 Feb-05-2022, 10:04 AM
Last Post: ibreeden
  Importing a function from another file runs the old lines also dedesssse 6 2,479 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  Importing text file into excel spreadsheet with formatting david_dsmn 1 3,548 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  importing a CSV file into Python russoj5 1 2,917 Aug-02-2020, 12:03 AM
Last Post: scidam
  Importing data from a text file into an SQLite database with Python macieju1974 7 4,012 Jun-29-2020, 08:51 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020