Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue Importing Utils When Working in m01b_intermediate #8

Open
JulioPDX opened this issue Mar 9, 2021 · 7 comments
Open

Issue Importing Utils When Working in m01b_intermediate #8

JulioPDX opened this issue Mar 9, 2021 · 7 comments

Comments

@JulioPDX
Copy link

JulioPDX commented Mar 9, 2021

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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

@JulioPDX
Copy link
Author

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

@Barrow1990
Copy link

Barrow1990 commented Aug 8, 2021

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
Copy link
Owner

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants