I'm attempting to import a script from my Items file but I keeps on getting an error
from .Items.Quest1_items import *
gives
from .Items.Quest1_items import *
# ImportError: attempted relative import with no known parent package
# Process finished with exit code 1
Here my project tree, I'm running the script from the main.py file
Quest1/
|
|- main.py
|
|- Items/
| |- __init__.py
| |- Quest1_items.py
Remove the dot from the beginning. Relative paths with respect to main.py are found automatically.
from Items.Quest1_items import *
You can only perform relative import (ie., starting with a dot), inside a package that you import. For instance, imagine the situation:
project/
├ main.py
├ mylib/
├ __init__.py
│ ├ module1.py
│ └ module2.py
in main.py, you would have import mylib or from mylib import *,
but inside module1.py, you could have from . import module2, because here the . stands for mylib (which is a python package, because you imported it within main.py).
So, the solution is simply remove the dot, it's not useful in your situation.
To put it simply: if you use relative import, you can run the file you want to run with 'python -m your_module_path' on the two layers above the outermost file used by your code.
Like the following, if you want to run run.py, you need to go to two layers above it, then run python -m dir1.dir2.run(without .py).
.../dir1/dir2/
-test
-test1.py
from .test2 import *
-test2.py
-run.py
from .test.test1 import *
Related
I have the following folder structure,
└── project
├── A
│ ├── main.py
│ └── __init__.py
└── B
├── __init__.py
├── C
├── __init__.py
└── module_x.py
I want to import all the methods in module_x.py into main.py. I have tried
from ..B.C.module_x import *
But I get the following error:
ImportError: attempted relative import with no known parent package
I wonder what am I doing wrong? How can this be done using relative import?
from project.B.C import foo
from ...b.c.module_x import foo
However, relative imports are only meant to work within one package. If project is a package, then you can use relative imports here. If project is not a package, you cannot.
However, if you're running a script in / and doing something like import project.A.b.foo, then that relative import will succeed because project is now a package. In that case, the following two would be equivalent:
from ...B.C import foo
from project.B.C import foo
You must use the -m switch to run python modules as scripts:\
$ cd project
$ python -m A.main # note no .py
This tells python that A.main is a module - python will also scan the current working dir (project) and detect package B - this will make your imports work correctly.
I have the following directory structure:
.
├── main.py
├── package
│ ├── module.py
│ └── utils.py
└── utils.py
Inside package, I have (lots of) code in which all imports are relative to package, e.g. package/module.py contains import utils, and it expects to import package/utils.py (not utils.py).
All the code outside package expects imports to be relative to the root directory ..
This is causing an issue for me because if main.py contains import package.module and I have PYTHONPATH=., then package/module.py ends up importing utils.py instead of the desired package/utils.py (since it contains import utils).
How do I resolve this without having to rename scripts? I would like to install the code in package in a way so that I can import it in main.py without its imports clashing with my other files.
What I tried: I added a minimal setup.py file inside package and ran pip install -e . but that didn't resolve the issue.
Thanks a lot for the help!
Have you tried a relative import for Submodules?
So that in general you would use
import utils # Import ./utils.py
import .utils # Import relative ./<eg. package>/utils.py
That would cause Scripts under 'package' to always import their local utils.py
I am trying to make my own package so that I can use the files in a different folder. This package contains a few different modules and then the main module that imports all the others inside it. For example:
Folder
|- main.py
|- other.py
|- something.py
|- __init__.py
Inside the main.py I have the imports:
import other
import something
and it works just fine when running the file itself; however, I added the __init__.py file and tried to import it into a different folder. The package is recognized, but the main.py gives me the following error:
Exception has occurred: ModuleNotFoundError No module named
'univariate'
File "C:...\stats.py", line 8, in
import univariate
File "F:...\testing.py", line 7, in
from stats import stats
For clarification, the actual main file is called stats.py. This is my first experience trying to make a package so I might be missing something. Thank you.
You need to change your imports into relative imports
import .other
import .something
or to change it to absolute imports rooted to your project folder
import x.y.other
import x.y.something
you can read here about the imports
When you have a module that you're trying to import you don't need the ".py" part.
Having a folder with a init.py file (even a blank one) means that a project that contains that folder can import from it.
/myproject
| - /mymodule
| - |- stats.py
| - |- other.py
| - |- something.py
| - |- __init__.py
| - main.py
then in main.py all you need to do is import mymodule or from mymodule import stats
I always hate to FTFM someone, but here's a link to how to build packages from the official documentation. But, where this really starts to shine is when you need to package your module so that someone else can run it Digital Ocean has a pretty good tutorial here.
Python project structure:
src/
- package-name/
-- A/
---B/
b1.py
---C/
c1.py
In c1.py, it uses a function defined in b1.py. I try 2 ways:
Method1: from src.package-name.A.B.b1 import b1_func
Method2: from ..B.b1 import b1_func
The import module starts from package-name directory, so src/ will not be visible in the imported module. So Method1 not working when import my own module.
Method2 is not working when run in IDE. ValueError: attempted relative import beyond top-level package
Any suggestions? thanks.
Do you have __init__.py files in A and B? It may be worthwhile to properly import b1_func into B's and then A's init files.
B __init__.py
from .b1_file import b1_func
or whatever
and A __init__.py
from B import b1_func
Then you should be able to import ..b1_func
I change the "Content Root" to the package-name directory in PyCharm and import package-name.B.b1. It works.
follow these steps to import the packages wherever u want
First of all, add __init__.py in all folders
i.e: __init__.py in src and __init__.py in package and __init__.py in A
and __init__.py in B and __init__.py in C.
If u want to import the functions from b1.py in c1.py add these lines in c1.py file.
import sys
sys.path.append(“../”)
#if u want from src folder add ../../
from B.b1 import YourFunctionName
I'm working on a Python application consisting of a core and multiple independent modules using the core. I'm having difficulty setting up relative imports of packages.
app
|- __init__.py
|- core
|- __init__.py
|- corefile.py
|- module1
|- __init__.py
|- main.py
The __init__.py files are empty. I'm running Python 2.7.1.
main.py
from .core import *
Running python main.py results in ValueError: Attempted relative import in non-package.
Similar questions: Ultimate answer to relative python imports, How to do relative imports in Python?, Relative imports in Python
Thanks for the help.
In short, you can only use relative imports from packages that are, themselves, imported.
For example, if you had:
$ cat run.py
from app.module1 import main
main.main()
$ python run.py
Then you could use a relative import in app/module1/main.py (although it would need to be from ..core import foo, because core/ is one level above main.py).
import sys
abs_filepath = '/home/n/Documents/IMPORTANT/deep_learning/drori_2018/ final_proj/Ryans_branch/StackGAN/'
# insert your absolute filepath above as abs_filepath = '/path/to/targ/dir'
sys.path.append(abs_filepath)
Please correct it if there are problems with doing the import this way
Other Answers:
Also please see here for a thorough answer about what's going on.