python importing path not matching windows 10 - python

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.)

Related

How to run Python interactive window with the -m flag in VSCode?

Explanation
VS code has this functionality named .py interactive window, which basically is jupyter notebook cells inside a .py file.
This makes my work much more efficient and easier because I cant test my modules like in Jupyter Notebook, use it as a module like .py files and also invoke it as script using python myscript.py. More detailed description here:
https://code.visualstudio.com/docs/python/jupyter-support-py
Problem
Right now if i want this setup to work I have to make imports by using:
sys.path.append("path/to/my/module.py")
Is there a way I can run my modules in interactive window with the -m flag so it doesn't fail when doing relative imports?
If not, is there another standardised way that also enables me running the script using VS Code jupyter cells inside .py files without having to import packages using sys.path.append
What I have tried
Until now I have tried adding ` __init__.py ` file to each subfolder but it still doesn't work, it fails when using relative imports from interactive window.
Any help, is appreciated.

Pycharm run the code adding the current working dir to PYTHONPATH

Trying to figure this out, because there is an inconsistency between when I run the code from Pycharm and from terminal.
Pycharm add automatically the current working directory; so if I add a module that is contained in my CWD, that is not in Pythonpath, it works just fine.
But when running from terminal, Python does complain, because my import statements refer to modules that are not reachable, because the CWD is not added to PYTHONPATH (I did verify this printing out the content of the variable, while running from Pycharm and from Terminal).
So at this point I am assuming that in my startup code, I need to add to Pythonpath the current directory, or this is not correct? I have no way to tell where the final user may put my code in; I just assume that the whole directory containing all my different modules, is located in a specific place.
To be more specific, this is where I am at:
my CWD when I run from Pycharm is /apps/myapp/logic/, I run the script after cd in that directory, and I call the script with ./myscript.py
The script has the #!/usr/bin/python3 line as first line, instead of running it with python3 -m myscript.py
The error I get, is when loading a module that is either in the same directory of my script (/apps/myapp/logic/) or one level above (/apps/myapp/); sadly the module load happen before my __main__ is running; so I can't add to sys.path the current directory from which the script run.
All these issues are not happening if I just run the script from Pycharm
After various trial and error, and thanks to the info that I did get from the comments; I did find 2 ways to solve the issue.
1) Create a shell script or another python script, which is adding the current directory (where all the files lives), and have no import in this file. Then the script call the script that has the main function.
2) On the top of the module, right after import sys, add the statement to add the path, in this way the current directory will be added to the PATH and it will be accessible, when the import try to access the module.
Neither look very nice, but this is all that I was able to find, to solve the issue. Pretty sure there is a more elegant way to do so

Reimport modules: Reload not working

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.

Running PyCharm project from command line

I am trying to deploy my project to a server and run it there.
When I try to start a script from command line it shows errors
when importing scripts that are in parrent directories.
I made the project (python 2.7.10) using PyCharm and it is spread out into multiple directories.
The folders look something like this:
project/dir/subdir/main_dir/script1.py
from dir.subdir.other_dir.script2 import * //gives error here
project/dir/subdir/other_dir/script2.py
def my_function():
//do something
I run the script by going to the main_dir and running: python script1.py
If you are running your script from the main_dir, that means when running your Python command, your relative reference is main_dir. So your imports are with respect to main_dir being your root.
This means if we take your script1 for example, your import should look like this:
from other_dir.script2 import *
Chances are your PyCharm project root is actually set to run from
project/
Which is why your references work within PyCharm.
What I suggest you do is, if your server is supposed to run within main_dir then you should re-configure PyCharm so that its execution root is the same in order to remove this confusion.
An alternative solution to this problem in my case was to add a main.py script in the root of the python project which triggers the program.
project/__main__.py:
from dir.subdir.other_dir.script2 import * //doesn't give errors
This means that when calling the program from the terminal the workspace would be correct and every inclusion of script would have the folders mapped correctly (from the root).
project/dir/subdir/main_dir/script1.py:
from dir.subdir.other_dir.script2 import * //also doesn't give errors
Another solution where you can skip the parent directories while importing (and don't have have to change anything in your script going from a Pycharm execution to a manual execution):
from script2 import *
works when you set the PYTHONPATH variable before running your script, e.g. like this in Windows:
set PYTHONPATH=../other_dir && python script1.py
for Linux (bash) it is:
PYTHONPATH=../other_dir python script1.py
I believe this is also what PyCharm does upon execution: adding the according folders to the PYTHONPATH.

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
"

Categories