User defined modules storage

I am pretty much new to Python and cannot quite understand the scenario below.

Firstly, there exists no module so the error below is understandable.

>>> import module1

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import module1
ImportError: No module named module1

Then I created the module and the sentences below worked perfectly.

module1.py

def func():
    print 'this is a test module'

Python IDLE

>>> import module1
>>> module1.func()
this is a test module

On the execution of the first statement, module1.pyc file was created. Then I added another function to module1.py.

module1.py

def func():
    print 'this is a test module'

def func1():
    print 'this is func1'

and tried to import this new file and run the statements below. But error was thrown as follows.

Python IDLE

>>> import module1
>>> module1.func1()

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    module1.func1()
AttributeError: 'module' object has no attribute 'func1'

I then deleted module1.py from the folder and then re-saved module1.py (with no contents changed). Again I executed the above statements in the IDLE prompt and the same error was thrown. And this time when the import module1 statement was executed, module1.pyc file wasn't created unlike the previous time.

However, on restarting the IDLE everything works perfectly but why doesn't the IDE recompile the module1.pyc everytime import statement is executed(without restarting or opening another IDLE window) or could anyone explain what exactly happens in the memory when import statement is executed everytime.

Thanks!!

Answers


If you import a module that has already been imported, it will not "re-import" and you will just get a reference to the original module.

You should use reload to re-import the module if it has been already imported:

reload(module1)

Need Your Help

Populating Highcharts chart with data received from MySQL

php mysql ajax highcharts

I'm new to web programming and this thing here is for my bachelor's degree project. I am doing an electrical energy meter and I want to see the measured data between 2 dates I choose, data which is

If scrolled 300 px change background color

javascript jquery css

I have a fixed nav that is grey. But I want it to change colour to white if you have scrolled 300 px.