Python: import from other directory - name is not defined - python

I looked to several related questions:
Importing python file from other directory
and
how to import module from other directory in python?
but they do not really solve my problems.
So I have
|-1.py
|-my_app
|-a.py
|-b.py
From 1.py I did:
import sys
sys.path.insert (0, './my_app/')
from a import *
and I have the error: name a is not defined.
How could I call the class and functions I defined in a.py and b.py from 1.py?
Many thanks

You need to have an __init__.py file (it can be empty) under the my_app directory for it to be an importable package.

Related

ImportError:How to import function or class from parent directory [duplicate]

This question already has answers here:
How to fix "Attempted relative import in non-package" even with __init__.py
(22 answers)
Closed 8 months ago.
I am trying to import a class from parent directory to my script but I receive attempted relative import with no known parent packageerror, I have searched everywhere in the internet but still cannot fix the error. Here is my package structure:
A/
a.py
__init__.py
B/
__init__.py
c.py
Now assume I have class1 inside a.py module and I am trying to import it in the c.py module. I have tried:
from ..A.a import class1
But i get above error message I have tried to add the A folder to sys.path but still the same error. Can anyone explain how I can import a package from a function or class from parent directory to subdirectory (like from a.py to c.py)
The way I do this is my extending the path in the c.py file by appending it. Below is an example of this:
a.py (example module in the parent directory)
class class1:
def __init__(self):
print("This worked!")
c.py (example file invoking a.py in the parent directory)
# By importing the sys module, you can change many
# things including the system environment.
import sys
sys.path.append("../")
# Once you've added the parent directory, you can freely
# import as you would have normally.
from a import class1
instance = class1()
The reason why your .. didn't work is because imports starting with . or .. are for importing from within things being imported (the way I understand it). An example of this to use your example would be to add a d.py alongside a.py which could be imported from a.py by using from .d import *.

Import from a parent's parent package (Python)

The following is my folder structure:
/experiments
/experiment_1
/experiment_2
/experiment_3
/src
/sample.py
helper.py
All experiments share some code, which I have exported into helper.py. Now I'm trying to import a function from helper.py into sample.py:
from ...helper import my_function
and I get the following error:
ImportError: attempted relative import with no known parent package
Please note that I have tried many solutions offered here, but none worked.
create a __init__.py file in your parent folder.
The above question is related to
Importing files from different folder
The solution is to add the following lines in the sample.py:
import sys
sys.path.insert(1, 'path-to-experiments')
from helper import get_json

Python: How to import a class from a parent directory

I am running a python-script that imports a class that I have created. This is the directory order:
Classes
- myClass.py
Scripts
- myScript.py
The first line of myScript.py reads:
from Classes.myClass import myClass
I have also tried:
import sys
sys.path.append('..')
from Classes.myClass import myClass
And I have tried to include an empty __init__.py file to the Classes folder.
With any of these ways, I end up with the error message:
" ModuleNotFoundError: No module named 'Classes' "
Can anyone help me?
From your top level directory
(I'm assuming it's the one containing Classes/ and Scripts/)
run: python3 -m Scripts.myScript
From man python3:
-m module-name
Searches sys.path for the named module and runs the
corresponding .py file as a script.

importing python module using relative name

I'm having difficultly with important a python module from another folder. Here's how my folder looks currently
foldername/
__init__.py
A/
__init__.py
spam.py
grok.py
B/
__init__.py
foo.py
I'm trying to import the functions and classes from the grok.py file into the foo.py in B. This is how my foo.py looks like
from ..A.spam import func
However, I get the following error:
ValueError: attempted relative import beyond top-level package
Could somebody help me? I don't understand where I'm going wrong
You can't use '..' like you do on the command line. You have to add your 'A' folder to your Python path. You can use sys.path.append('/dir/of/A') and then from A.spam import func
Instead of using sys.path you could also add a *.pth-file to your python or anaconda "site-packages"-folder which contains the path to the folder "A".
Import via from A.spam import func as #bikemule already proposed.

ImportError: No module named utils.read

I have a main.py file in the folder project and read.py in the folder ./project/utils. In the main.py, I called
import sys, os
sys.path.append('./utils/')
from utils.read import function1
However, when I use the python main.py command, I got the error
ImportError: No module named utils.read. What should I change? Thanks all
i think you need to add __init__.py
in your directory..
make sure you have __init__.py in utils folder.. then only python will understand it is package folder contains py
__init__.py specifies that the folder is actually a package. a package in python is a directory containing .py files (modules). In every folder which is a package (that is, a folder containing multiple .py files) you should define __init__.py.
It can be empty, or you can put some statements to execute when the package is imported (or modules from the package).
For exmaple, let's take this directory tree:
/dev/package/greeter.py
and current working directory is /dev.
>>> from package import greeter
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
from package import greeter
ImportError: No module named package
import pakcage results in the same error. When adding __init__.py into the package folder, it works. My init is simple as
print 'init executed'
>>> from package import greeter
init executed
>>>
One common functionality to put in __init__.py is the __all__ variable. You can read more about it here Can someone explain __all__ in Python?

Categories