IKA
IKA

Reputation: 161

Import python modules from files

How do I import modules from *py file? I have file pythonScript.py with lines: import os, sys. If I import the file pythonScript these lines does not execute. Usung one on these modules I'm getting name error.

Upvotes: 0

Views: 102

Answers (1)

Andrew Guy
Andrew Guy

Reputation: 9968

It sounds like you haven't fully understood the way imports work.

If you run:

import my_module

then anything contained in the my_module.py module is namespaced to my_module. So to use this module, you would have to type:

result = my_module.some_function(1,2,3,4)
object1 = my_module.SomeClass()

Similarly, if you really want to use the os import from my_module, you have to access it by:

my_module.os

If you import using:

from my_module import *

then you can access os directly, but it is worth noting that wildcard (*) imports are not recommended.

Just import os again in your python terminal.

Upvotes: 1

Related Questions