chuckablack / python-52-weeks

repository for teaching python
MIT License
107 stars 76 forks source link

Issue Importing Utils When Working in m01b_intermediate #8

Open JulioPDX opened 3 years ago

JulioPDX commented 3 years ago

I'm running Windows 10 and Python 3.8.2 In case you face a similar issue when starting the generators video. My path to util.create_utils could not be found. I had to add the path when executing the script. Please see below

12_generatory.py

import os, sys
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
from m01_basics.util.create_utils import (
    create_device,
    create_devices,
    create_devices_gen,
)
import time

if __name__ == "__main__":
chuckablack commented 3 years ago

Ah yes. I'm running all the time in PyCharm for creating the lessons, and have 'm01_basics' marked as 'sources root', so that directory was added to the PYTHONPATH.

When running from a terminal, I set PYTHONPATH equal to "../m01_basics" and it was happy. I personally would probably prefer that over the extra code you provided, just to keep the code cleaner.

Of course what I should have done was put that 'util' module in a general location and made sure it was along the PYTHONPATH. So thanks Julio for finding that and making me aware of it!

JulioPDX commented 3 years ago

Hey Chuck,

Thank you very much for the reply. Yeah the extra code is probably not the best approach but in a pinch for students I'd figure to post it. Thank you for all the lessons!

chuckablack commented 3 years ago

And thanks for finding this, figuring a workaround, and letting me know so I can chime in with advice as well! :-)

JulioPDX commented 3 years ago

I can't take the credit. Have to thank the internet's for that! See here solution

Barrow1990 commented 3 years ago

Thanks for the solution. For Ease of coding i have put that file in the mo1b_intermediate directory. So the file l_09_sys_import.py has the following in:

import os
import sys
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)

and then at the start of the l_12_generators.py file i have the following: import l_09_sys_import

Just thought someone else might benefit off it

chuckablack commented 3 years ago

Thanks for doing that. As an aside, if you have that file in your directory, I don't think you need all the currentdir and parentdir stuff - isn't the local directory always searched? I'm not 100% sure about that, but it seems un-Python-like to require that you do all that directory-finagling just to import a module from your own directory.

Barrow1990 commented 3 years ago

Thanks for doing that. As an aside, if you have that file in your directory, I don't think you need all the currentdir and parentdir stuff - isn't the local directory always searched? I'm not 100% sure about that, but it seems un-Python-like to require that you do all that directory-finagling just to import a module from your own directory.

I have the l_09_sys_import.py inside the m02_intermediate directory in which the utils is in the m01_basics. So to get around the above error on my system i just have that file imported into the generators file (Also for any other file that i may require in the future - still going through the course) I find the code in the generators becomes neater with it just as an import. I also read somewhere that yes the packages inside the directory are auto imported but anything up a level isn't anymore and you have to change the level. Think it changed in Python 3 or a revision of Python 3.x I could be wrong but the workaround makes it so i can run the code