Python files | import errors | How to avoid & fix Import errors and running as a Python script

Publié le: 02 mars 2023
sur la chaîne: Sohaib Jahangir
82
7

Python files | import errors | How to avoid & fix Import errors and running as a Python script
#pythonfiles #python_import_errors #pythonscript #pythonforbeginners

In this video, we're going to look at:
what is an import error, and also how you can run some

code in a file, only when the file is ran as a script

and not as a module.

It's very straight forward.

The import errors, though, are not so easy.

Let's have a look at an import error.

We've got app importing file operations,

file operations importing find,

and find's not importing anything.

Let's make find import file operations.

So let's do this, let's ask it to go into file operations

and import something specific.

Let's run app.py, let's see what happens.

Now we get an import error because what's happening

is app is importing utils.common.file_operations,

this is importing utils.find,

and now utils.find is importing utils.common.file_operations

which is already in the imported modules.

And Python knows about this, it keeps a global dictionary

of modules that have been imported already,

and as we try to access something in it,

it says, oh wait, actually, when I try to go

to this module to extract this function here,

I can't do that because when I run that,

it asks me to go back here, so that doesn't work.

Notice however, that if you ask it not to import

a particular thing, but just to execute the module,

like if you do import utils.common.file_operations,

it is okay with this because it sees

that its already imported this module,

therefore, nothing to do.

It just ignores it, because its already been imported

in the past, like this.

So that's a pretty important thing to know.

If your module's already imported,

you can import it again the same way,

but if your module's already imported,

and you want to access something specific in it,

Python is going to then go and look into that module again,

and it's going to give you an error

because it's going to go back and forth.

This is called a circular import,

it's not such a good thing to do.

Now, in some cases, a completely separate topic,

you'll want some code to run,

for example, let's go to the find function.

You'll want some code to run only when

you execute find.py.

So if you run find.py, you want some code to run,

and you can do that.

By the way, I'm going to delete this import there

because it works, but it's a circular import,

and I don't like that.

So if we were to run find.py,

let's say we wanna test out our finding function.

So we'd say, find_in Rolf, Jose, and Jen,

and what we're looking is lambda x,

yeah, that's it.

We're gonna just return x, and the expected is, Jose.

So what's gonna happen is, this list becomes the iterable

lambda x returns x, which returns every element

as we go through them,

and Jose is the value we're looking for.

So for i in iterable, goes over each element.

i becomes Rolf, then it becomes Jose, then it becomes Jen,

we run the finder function, which just gives

us the value that's Rolf, Jose, or Jen,

and we check that it's equal to the expected value,

which in this case is Jose.

i would return it if it's fact.

So if we run the find.py file,

we get Jose out, which is good.

But notice how find.py is imported by file operations.

File operations is imported by app.

So if we run app, we also get Jose,

because find was executed by the import

and it ran this code.

We don't want that.

Because this is only for testing the module.

We only want to run this code if we physically

right-click the module and run it.

If we run it as a script.

And we can do that with an if statement.

Of course, what's gonna be in our if statement?

We wanna check whether the name is main.

Go and implement it yourselves.

I'm sure you got that if name is main, run this.

Pretty straight forward stuff.

Of course, you have to realise that

this is something you can do.

After all, these are just a variable.

This is just a variable, and this is just a string.

So you can check whether they are equal,

and run something if they are.

Now when you run find.py, you get Jose.

When you run app.py, you don't because it didn't run

as a script.

The name was not main.

The name was utils.find.

That's it for this video.

I wanted just to show you a bit more about importing,

how you can run something only when you run it as a script,

and also how you can cause import errors,

and of course, that you should avoid import errors.
python import error cannot import name,
no module named jupyter pycharm,
circular import error python,
python import not found,
run python script from command line,
no module named jupyter notebook,
#basicsofpython #pythonprogramming #pythontutorial #learnpythonprogramming #pythonprogrammingforbeginners #pythonbeginners #sohaibjahangir #python_by_sohaib_jahangir


Sur cette page du site, vous pouvez voir la vidéo en ligne Python files | import errors | How to avoid & fix Import errors and running as a Python script durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Sohaib Jahangir 02 mars 2023, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 82 fois et il a aimé 7 téléspectateurs. Bon visionnage!