There are lots of similar questions, but none of them seem to help me with this.
I have the following file structure:
rest_api
__init__.py
utils
__init__.py
config.py
foo
__init__.py
foo.py
I'd like to import the Config class from config.py, into foo.py. I attempt to run:
pipenv run python rest_api/foo/foo.py
I have tried numerous things, including adding the parent folder to PATH and using relative imports, but the import line always fails:
from rest_api.utils.config import Config
I get the following error:
ModuleNotFoundError: No module named 'rest_api'
I should mention that the __init__.py files are all empty. Not sure if they are neccessary, since I have Python 3.8.2.
I have the following structure:
app/
code/
script.py -- has a function called func
main.py
How can I import script.py from main.py ?
I tried from code.script import func and I got ModuleNotFoundError: No module named 'code.script'; 'code' is not a package
Place a __init__.py file in the code directory. This will allow your main.py code to import it as a module like you have tried there.
Indeed the best way is to add __init__.py in code directory because when a regular package is imported __init__.py file is implicitly executed and the objects it defines are bound to names in the package’s namespace.
FYI, as an alternative you can also to this in your main.py before the import:
import sys
sys.path.append("/path/to/script.py")
I have the following project structure:
xxx
├── xxx
| ├── settings
| └── tests
| └── __init__.py
| └── conf.py
| └── xxx.py
| └── utils.py
Those are the imports that I have in each file.
xxx.py
from xxx import utils, conf
from xxx.conf import BASE_DIR
conf.py
import os
import yaml
utils.py
import os
import shutil
from typing import List, Optional, Tuple, Dict, Union
import requests
When I run my app with
python3 xxx.py
I get the following error:
ImportError: cannot import name 'utils' from 'xxx' (/Users/yyyyy/workscpace/xxx/xxx/xxx.py)
When I run my test suite with pytest, I don't get any errors.
I have tried to change the import to (because those files are modules in my package):
import utils
import conf
In this case, I can run my app with no errors but I get the following error when trying to run pytest
ModuleNotFoundError: No module named 'utils'
What am I doing wrong?
Your question is very similar to ModuleNotFoundError: No module named x
When you run python3 xxx.py, it becomes the main module, so it's looking for something like xxx/xxx/xxx/utils.py.
When you run pytest, it is aware of the xxx package, and therefore picks up the utils and conf as you expect.
If you use absolute imports, the tests fail because python is now looking for utils on sys.path. But python3 xxx.py works because the local folder is added to your sys.path.
A clean solution would be to create a main.py one level up from utils.py and conf.py and import your code from there, like:
# main.py
from xxx import utils, conf
def main():
# do stuff here
if __name__ == __main__:
main()
When I run my app with
python3 xxx.py
This means that your current working directory is inside the xxx package. There is no xxx.conf to discover, because the top level seen to python is at conf, utils etc.
Run your app from the parent folder using the -m switch:
python3 -m xxx.xxx
Note that it is a good idea to avoid naming sub packages like the main package. This avoids confusion, especially when you or someone else has to maintain your package in the future.
Is your package's name xxx.py?
If those two have the same name, Python will import the xxx.py not the package xxx because Python looks for packages in sys.path sequentially, and the first entry is usually the current directory (of the project).
Otherwise, if you only have python xxx.py, sys.path will have current path in xxx package, but if you use pytest import xxx, the current not in xxx package, python will not find package or .py in xxx package and can't find utils.py and conf.py
in this case you can try to write from . import * in _ init _.py or in xxx.py write from . import utils, conf
I have a ROS package that I am working with and I am trying to import a python module from another directory in the same package. My file structure is the follow:
package/
src/
__init__.py
lab03/
map_helper.py
__init__.py
lab04/
foo.py
__init__.py
I want to use helper.py in foo.py
foo.py
from src.lab03 import map_helper as helper
However I am getting the following error:
from src.lab03 import map_helper as helper ImportError: No module named src.lab03
You need to add package directory to your sys path to be able to import packages
import sys
sys.path.append('../../../package')
from src.lab03 import map_helper as helper
Have you tried this?
from package.src.lab03 import map_helper as helper
I have this file structure:
.
test/db_test.py
models/user.py
I want to import user.py in db_test.py, for example i try it:
from ..models.user import User
but have this error:
SystemError: Parent module '' not loaded, cannot perform relative import
How can do this work?
all path have __init__.py file
i don't want to use appending in sys.path
thank for your answers
Have you tried running the script as a package? Try running the following from the directory containing your package root directory:
python -m your_package_name.test.db_test
My test that this worked for was:
your_package_name/
__init__.py
test/
__init__.py
db_test.py
models/
__init__.py
user.py
Where "db_test.py" contained:
from ..models.user import User
So I ran that command from the parent directory of "your_package_name".