#๐Ÿ”’ Writing to file with pprint and then importing as a module results in ModuleNotFoundError

43 messages ยท Page 1 of 1 (latest)

sour plume
#

Here's my code.

from pathlib import Path
import os
import pprint

os.chdir("E:\New Stuff\Documents\Misc\Python Tutorials\Text Quest\Game Files\Save File 1\Character Save File\")

save_file = open("MySaveFile01.py", "w")
stats = [{"str": 5, "dex": 5, "agi": 5, "end": 5}]
save_file.write('stats = ' + pprint.pformat(stats) + "\n")
save_file.close()

import MySaveFile01

I'm using "Automate the Boring Stuff with Python" as a reference and in chapter 9 on reading and writing files, they say that you can write to a .py file with pretty print to create new files with python code in them and then import that code back into your program. I'm trying to use this to create a save/load system for a game I'm making. The file directories are correct, and the file is created properly. The file contains syntactically correct python code. When I try to import it, it gives me an error saying that there's no module named "MySaveFile01". Why can't I import the file?

amber creekBOT
#

@sour plume

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

slate marlin
#

import looks next to where your script is

#

it looks like you're writing the file somewhere else via os.chdir()

#

A trick that may help you, not sure

#

!e ```py

with open("save.py", "w") as f:
f.write("a = 1")

with open("save.py", "r") as f:
save = f.read()
namespace = dict()
exec(save, namespace)

print(namespace["a"])

amber creekBOT
sour plume
#

I'm going to be completely honest, I have no idea what you just did.

#

Can you break that code down for me a little?

slate marlin
#

Sure

#

with open() as f is the same as:

f = open()
# code in the :
f.close()
#

save = f.read() reads the python format save file into a string

#

so, in this case, save would be "a = 1"

#

!e ```py
namespace = dict()

exec("a = 1", namespace)
print(namespace["a"])

amber creekBOT
slate marlin
#

exec() lets you run python code right where you call it:

#

stands for execute

#

!e ```py
exec("a = 1")
print(a)

amber creekBOT
slate marlin
#

you can pass a second argument to execute within a dictionary instead of right where you are

#

so, namespace["a"] would be the same as save_file.a

#

just done via file open, exec, and a dict instead of import

#

Please do ask questions if you want

#

I suggested it because there's no real good method to importing a file from an arbitrary location in python, mostly by design

sour plume
#

Ok, so you just write to the file normally, then you read the file normally, creating a string with your code in it, then use the exec() function to run the code inside the string?

slate marlin
#

I suspect the book assumes you'll put the save file right next to your code and can just import like that

#

exactly

#

and you pass in a dictionary to run the string code within instead of within your regular space

#

so a = 1 sets a inside the dictionary instead of where you called exec

sour plume
#

When you say "pass in a dictionary", is that the dict() function?

slate marlin
#

dict() makes a dictionary, just like {} does

#

I used it to try and be more clear but I think it had the opposite effect :P

sour plume
#

So it's like when you have int("5") but with a dictionary

slate marlin
#

!e ```py
save = {}
exec("a = 1", save)
print(save["a"])

amber creekBOT
slate marlin
#

yep

#

!e ```py
a = int()
print(a)

amber creekBOT
sour plume
#

Ok, that makes sense. Thanks for your help!

slate marlin
#

ofc! hopefully didn't send you too far into the weeds :P

sour plume
#

!close

amber creekBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.