Reputation: 63
I want to include all files in my_lib
as the directory will contains most used function per library. In this case i want to import my_pandas.py
.
I already add the library directory my_lib
which contains __init__.py
and my_pandas.py
.
However i cannot run the following command:
from my_lib import my_pandas
How can i run from my_lib import my_pandas
command?
Upvotes: 0
Views: 705
Reputation: 1
I found this one works best for me, just two extra lines in code and no need to modify any environment variables.
import sys
sys.path.append('/path/to/my_lib')
import my_pandas
Upvotes: 0
Reputation: 2358
Finding your own custom modules in Python is unfortunately a little fiddly.
You have correctly made my_lib
into a module by adding __init__.py
. There are two ways to get the module to import correctly, but they boil down to the same thing: the module needs to appear as a subdirectory inside a directory that is in your path.
Therefore your solutions are:
/home/inetvmart/python_apps
and run your notebook there instead of inside /home/inetvmart/python_apps/my_lib
(the current directory is always included in the path, and my_lib
will be a subdirectory inside your current directory in this case)/home/inetvmart/python_apps
rather than /home/inetvmart/python_apps/my_lib
to your path (then the parent directory will be in the path. Therefore your custom module will be a subdirectory in a directory that is in your path)In this particular case, then, you can probably just change your second cell to: sys.path.append("/home/inetvmart/python_apps")
I must warn you that this is a somewhat ... hacky ... approach (modifying the path inside a Python script to ensure your imports succeed).
In the medium term, you may like to do something like:
/home/<your username>/my_python_modules
$PYTHONPATH
environment variable (which you could set in your .bashrc
/.zshrc
/other file)Then you will be able to import the custom modules inside my_python_modules
from anywhere on your machine.
Upvotes: 0