This question already has answers here:
How do I import other Python files?
(23 answers)
Closed 5 years ago.
Can I import the test01.py like os, sys module?
I want to import the test01.py like:
import test01.py
In this case now, I only can import it like this:
from testDemo02 import test01
Is it possible to reach it?
It looks like test01 is in the package testDemo02 - you can tell because there is a file __init__.py in the directory testDemo02. Given that, there are a couple possibilities:
If the parent directory of testDemo02 is in the module search path (sys.path), but testDemo02 itself is not, you can import your test01 module using either
import testDemo02.test01
or
from testDemo02 import test01
I suspect this is the case since you tried the latter one and it works. This is what I would expect because I see that __init__.py file there.
If testDemo02 itself is in the search path, you will be able to import your module with just
import test01
I would find it strange for a directory to be in the search path when it also contains an __init__.py file, but it is possible.
You can use sys module's path property to append your path:
>>> import sys
>>> sys.path.append("/testDemo02/test01")
>>> import test01
Related
I have spent so many hours trying out different answers on stack overflow that i no longer know what exactly is the proper way to use relative imports. Keep in mind this import should work on localhost and on a server
My project structure
init.py
Attempts to import Authenticate class in main.py result into ImportError: attempted relative import with no known parent package
Kindly give an explanation or links with working examples of importing in the same directory.
You are trying to import a Jupyter Notebook, not a class. This is why you get the ImportError.
Have a look at this: ipynb import another ipynb file
If you do not want to import from a Jupyter Notebook but from a module in a specified path you can try this:
import importlib.util
spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py")
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
foo.MyClass()
You can also use relative import:
from foo import bar
Another option is to add a path to sys.path (over using import) to simplify things when importing more than one module from a single package:
import sys
# my_package dir contains mod_one.py, mod_two.py and mod_three.py
sys.path.append('/foo/bar/my_package')
from mod_one import foo
from mod_two import bar
from mod_three import foobar
This question already has answers here:
Relative imports in Python 3
(31 answers)
Closed 4 months ago.
from ..box_utils import decode, nms
This line is giving error
ImportError: attempted relative import with no known parent package
What is this error and how to resolve this error?
Apparently, box_utils.py isn't part of a package. You still can import functions defined in this file, but only if the python script that tries to import these functions lives in the same directory as box_utils.py, see this answer.
Nota bene: In my case, I stumbled upon this error with an import statement with one period, like this:
from .foo import foo. This syntax, however, tells Python that foo.py is part of a package, which wasn't the case. The error disappeared when I removed the period.
If a different dictionary contains script.py, it can be accessed from the root. For instance:
If your program is structured...:
/alpha
/beta
/delta
/gamma
/epsilon
script.py
/zeta
...then a script in the epsilon directory can be called by:
from alpha.gamma.epsilon import script
in the latest python version, import it, directly don't use .. and .library
import the file which you want. this technique will work in the child directory.
If you import it from parent directory, then place the directory's full path.
package
|--__init__.py
|--foo.py
|--bar.py
Content of bar.py
from .foo import func
...
If someone is getting the exactly same error for from .foo import func.
It's because you've forgot to make it a package. So you just need to create __init__.py inside package directory.
This question already has answers here:
How to do relative imports in Python?
(18 answers)
Closed 6 years ago.
I have a package with the following hierarchy
my_package/__init__.py
script_a.py
scripts_dir/__init__.py
script_b.py
my_package/__init__.py
module_a.py
module_b.py
module_a and module_b contain function and class definitions that I am using in script_a and script_b (which are stand alone scripts and contain a main)
When I import something from let's say module_a.py in my script_a.py everything is fine.
My problems are
I cannot figure out how to use relative imports to import something from module_a or module_b to script_b.py
I am not sure if I am supposed to use relative imports or if it makes more sense to add my_package to sys.path and then use something like
from my_package.module_a import the_funky_func
I want to avoid having to call the interpreter with the -m argument
update
From the answers I have found so far in SO I have concluded that I have 3 options
write a setup to include the package to my PYTHONPATH so that all scripts regardless of where they are can call the modules
use the -m argument when invoking the interpreter
do some sys.path hack
Is there another option that I am not aware of?
myproject/
|--package1
|--\__init__.py
|--script_a.py
|--script_b.py
|--package2
|--module_a.py
|--module_b.py
You can use below 2 lines appends myproject path to sys path. It can avoid relative import and avoid -m in the command line
import sys
import sys.path.append("/absolute/path/to/your/myproject")
In myproject file script_a.py, if you import module_a.py it would look like
import sys
import sys.path.append("/absolute/path/to/your/myproject")
import package2.module_a as ma
This question already has answers here:
Import python package from local directory into interpreter
(10 answers)
Closed 9 years ago.
I'm writing an installation program that will pull a script out of an existing Python file and then use it in the main Python program. What I need to know how to do is import <file> from the current working directory, not the standard library or the directory the main code is in. How can I do that?
This works:
import os
import sys
sys.path.append(os.getcwd())
import foo
import sys
sys.path.append('path/to/your/file')
import your.lib
This will import the contents of your file from the newly appended directory. Appending new directories to the Python Path in this way only lasts while the script is running, it is not permanent.
You should be able to import the module from your current working directory straight away. If not, you can add your current working directory to sys.path:
import sys
sys.path.insert(0, 'path_to_your_module') # or: sys.path.insert(0, os.getcwd())
import your_module
You can also add the directory to PYTHONPATH environment variable.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python package structure
Hello,
I'm looking to import a python file that I have in a sub-directory that is below the root of my main file. e.g.
import ../library/utils.py
When I put that into my code and run it I get a compile error.
Is there a way to include files from below the main file root directory or does it have to be in a sub-directory in the root?
Thanks for your help.
You don't import files, you import modules. Modify sys.path accordingly, and do import utils, e.g.
import sys
sys.path.append('../library')
import utils
import sys, os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'library')))
import utils
This will modify the sys.path variable that contains the directories to search for files. It will also make sure that it will find it properly even if you run it as:
$ python the_file.py
$ python ../the_file.py
$ python /somewhere/over/the_file.py
This will work for stuff under development, testing, training. Installed files will not need such a construct.