1

I've looked on many sites and many related questions, but following the solutions to those questions still didn't seem to help. I figured maybe I am missing something, so here goes.

My project is to create a DM's tool for managing table top role playing games. I need to be able to split my project into many different files in order to keep everything organized. (so far) I have only three files I'm trying to work with. I have my main file which I called dmtool.py3, I have a file for class definitions called classdef.py3, and I have a file for creating race objects called races.py3.

1] The first of my questions is regarding importing singular files. I've tried organizing the files in several different ways, so for this lets assume all of my three files are in the same directory.

If I want to import my class definitions from classdef.py3 into my main file dmtool.py3, how would I do that? import classdef and import classdef.py3 do not seem to work properly saying their is no module with that name.

2] So I then made a module, and it seemed to work. I did this by creating a sub-directory called defs and putting the classdef.py3 and races.py3 files into it. I created the __init__.py3 file, and put import defs in dmtool.py3. As a test I put x = 1 at the very top of races.py3 and put print("X =", defs.x) in dmtool.py3. I get an error saying that module doesn't have an attribute x.

So I guess my second question is if it is possible to just use variables from other files. Would I use something like defs.x or defs.races.x or races.x or maybe simply x? I can't seem to find the one that works. I need to figure this out because I will be using specific instances of a class that will be defined in the races.py3 file.

3] My third question is a simple one that kind of spawned from the previous two. Now that races.py3 and classdef.py3 are in the same module, how do I make one access the other. races.py3 has to use the classes defined in classdef.py3.


I would really appreciate any help. Like I said I tried looking up other questions related to importing, but their simple solutions seemed to come up with the same errors. I didn't post my specific files because other than what I mentioned, there is just very simple print lines or class definitions. Nothing that should affect the importing.

Thanks, Chris

2
  • 3
    afaik it should just be *.py ... I dont think you can name the extensions py3,py2. Commented May 19, 2015 at 19:19
  • Are you aware that modules are just implemented with single files? defs.py instead of a defs directory. Packages use folders, and you'd need to import defs.races to load the races module from the defs package. Commented May 19, 2015 at 19:21

2 Answers 2

1

Firstly, do not use .py3 as a file extension. Python doesn't recognize it.

Python 3's import system is actually quite simple. import foo looks through sys.path for a package (directory) or module (.py file) named foo.

sys.path contains various standard directories where you would normally install libraries, as well as the Python standard library. The first entry of sys.path is usually the directory in which the __main__ script lives. If you invoke Python as python -m foo.bar, the first entry will instead be the directory which contains the foo package.

Relative imports use a different syntax:

from . import foo

This means "import the foo module or package from the package which contains the current module." It is discussed in detail in PEP 328, and can be useful if you don't want to specify the entire structure of your packages for every import.

Sign up to request clarification or add additional context in comments.

3 Comments

.py3 is not valid? Are you sure? I've used this for a project which worked perfectly. The files do run.
I ended up changing anyway. I reorganized my files to all be in the same directory. Using from . import races came back with an error saying "Parent module '' not loaded, cannot perform relative import." So I tried replacing the . with the name of the file everything is in, and that had an error saying that that module didn't exist.
I removed the from section entirely. import races worked fine. Everything works, thank you everyone!
0

Start python and type these commands:

>>> import sys
>>> sys.path

The path is the list of directories where python looks for libraries. If your modules are not on the list, none are found.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.