Reimport modules: Reload not working - python

I am a Matlab user and new to Python. I want to call the simplest python function from an external file. I edit the function, but my Canopy interpreter (IPython) does not recognize the new version and keeps calling the old file!
Say this function saved as mymodule.py:
def oper(x):
print(x)
The main file is:
del mymodule.pyc
import mymodule
import imp
imp.reload(mymodule)
oper(5)
Run this once. Change print(x) to print(x+1). It keeps executing print(x).
If I define the function oper(x) in the main file, or if I close Canopy and reopen it, or if I reset the Canopy kernel via GUI Run>Reset Kernel, it's all right (but I cannot program these).
I also tried reload(mymodule) and %reset, which do not solve the problem. Ideally, I want a Python command in the main file that would completely reset the workspace (kernels, namespctes etc). I am using Canopy 1.7.4.3348, which includes Python 2.7.11 and IPython 4.0.0.9.

You need to import like this and use like
from mymodule import oper
oper(5)
or
import mymodule
mymodule.oper(5)
Also, in the same directory of mymodule, create an empty __init__.py file there. That will let python know that it is a module.

Related

python importing path not matching windows 10

I im using Git bash to open jupyter lab and a notebook file. I want to import a file such as test.py, with a function such as test_func(x). The test.py is in another folder then the working directory. using pwd in the notebook i get something like "C:\Users\Documents\Code_folder\". I have added the path of the test.py using sys.path.insert(1, "C:\Users\Code\), where the test.py is located.
I then have no issues with importing the module, but if i add another module, test_func2(y), and i say run test.test_func2??, i cant find the function, and when running test.test_func??, i see that the output on line: File: "c:\users\code\". I belive is the lower case of the File that gets me the missing module.
Why does this happen, and can i change it in a simple say without changing all my codes?
Edit: test_func2 is another function in test.py
This may simply be an issue with how you're importing. I'm not sure of the internal mechanics of Jupyter, but in a terminal window if you change the module it has to be reloaded (reimported.) In Python3 the reload was moved to the imp module.
See stackoverflow:How do I unload (reload) a module?
For Jupyter, I assume you have the import test.py in a previous window. If you add a function to a .py file, just go back to that window and rerun the import...although I'm not sure that will guarantee a reload (since just re-running the command import test.py in the terminal Python would not work.)

Spyder not updating .pyc files

I have created the class in separate code in Python (Spyder). Then I import the class into main code using:
from othercode import classxy
Then the .pyc file is created in pychache' folder. However when I change something in the class, save it and import it once again in main code, then the .pyc file is not updated and main code is still working with old version of class. I have to delete the .pyc file for class and turn off and turn on the Spyder which is kinda dumb. Is there something I'm missing? When I run the main code in Anaconda prompt, everything is working as it should, just Spyder is behaving strange.
(Spyder developer here) To have your code updated after every change to it, you need to run these commands before running your code in our IPython consoles (but only once):
In [1]: %load_ext autoreload
In [2]: %autoreload 2

Eric5: Update module imports

I'm using eric5 for my python projects and I really like it.
However I don't understand how to force Eric5 to re-import modules before running a script.
Example of my workflow.
My modules
== mainfile.py ==
import mymodule
# some more code
=================
1.) First run of mainfile.py with "Start->Run Script".
2.) Then I modify mymodule.py and re-run with "Start->Run Script"
3.) I find out that the modification in mymodule.py has not included in the mainfile.py. There was no re-import of the updated file done.
4.) Currently I'm helping myself by closing and opening the entire Eric5 project. This works but is not very elegant. I assume that there is a more convenient way?

importing a module in Idle shell

I'm trying to learn python and I'm having trouble importing a module.
I have a .pyc file that I'm trying to import into idle shell called dfa.pyc
I have the file in a folder called xyz.
I navigate to this folder using:
os.chdir('/Users/xxx/Desktop/xyz')
So now, if I try to run the command:
from dfa import *
i get the error:
ImportError: No module named dfa
If i run the command:
os.path.isfile('dfa.pyc')
it returns true.
Can someone explain how i can get the dfa.pyc file imported?
Thanks
I don't think python modules are loaded I based on what you do with chdir. Modules are loaded from the folder you started the python shell and folders in PYTHONPATH.
If you want dynamically load modules maybe you can check imp.loadmodule (sample in the bottom of the page).
you can add to the PYTHONPATH in code by doing
sys.path.append('<newpath'>)
from dfa import *
I don't believe changing your current directory has any impact on the import process and even if it did, I'm not sure that's how you would want to do it.
from Brian Fitzgerald in Loading (and unloading) Python modules
"
... and this to “un-import”
del sys.modules["package"]
del package
"

Where to trigger ast modifications in Python?

I'm doing some AST modifications in Python. This may be done for optimization purpose or other.
Where is the right place to put it, so when you do something like:
python myfile.py
or
python runserver.py myapp
the modifications took place for every .py executed file?
Put your modifications in a site.py or sitecustomize.py (Python < 2.6) file (in the lib/site-packages directory ; Python will try to import and run it # interpreter startup).

Categories