Im trying to import a module from a different sub directory in Python 3 and I'm having trouble getting the script running. The current file structure I have is:
/lets_import
__init__.py
/app
__init__.py
main.py
server.py
/tests
__init__.py
tests_main.py
tests_main.py imports a function from server.py like this:
from app.server import add
(add is the function being imported from server.py)
init files are empty. They are there so that each sub dir is seen as a package.
When I run tests_main.py I get the following error
File "tests/tests_main.py", line 2, in <module>
from myapp.server import add
ImportError: No module named 'myapp'
I tried running it from the root level folder like this python test/tests_main.py but still got the same error.
Is there a way to do this without having to manipulate PYTHONPATH?
Related
I am a beginner with Python. Before I start, here is my python folder strcuture.
-project
---main.py
---secondary-folder
----------new_main.py
---model
----------__init__.py
----------abc.py
Under project folder I have a model folder which has two python files __init__.py and abc.py which contents follow:
class ABC:
def print_abc(self):
print("abc")
Next in my main.py and new_main.py are the same contents:
from model.abc import ABC
if __name__ == "__main__":
ABC().print_abc()
Whenever I run python3 ./secondary-folder/new_main.py under project folder it results in the error:
Traceback (most recent call last):
File "./secondary-folder/new_main.py", line 1, in <module>
from model.abc import ABC
ModuleNotFoundError: No module named 'model'
But the terminal can print out abc if I run main.py under project folder.
Is there anything I missed?
If you try to import something the code is seeking from the folder you have run the file in.
So in folder secondary-folder you have no module (folder) named model. You have to leave that folder with calling parent folder, project in this case.
Try this in new_main.py:
from project.model.abc import ABC
if __name__ == "__main__":
ABC().print_abc()
After I do some researches, I have got an idea from this link beyond top level package error in relative import
All project folders have to add ini.py like the structure below:
-project
---__init__.py
---main.py
---secondary-folder
----------__init__.py
----------new_main.py
---model
----------__init__.py
----------abc.py
Then run the command python secondary-folder.new_main Everything will be fine.
I wanted to import a file from another folder into another file in the same root folder.
Directory list:
ROOT/
resource/
console_log/
__init__.py (empty)
file1.py
libs/
__init.py__ (empty)
function.py
__init__.py (empty)
I tried using this code in resource/console_log/file1.py:
from resource.libs.function import function_name
It just doesn't work. Returns this error:
No module named 'resource'
If I use this code instead
from ..libs.function import function_name
It gives me this:
attempted relative import with no known parent package
How can I solve?
Is there any way to fix it without using the sys?
EDIT:
I "fixed" the problem by moving all the files directly to the libs folder thus removing the resource folder. Except that if from libs/file1.py I want to import a function present in the main.py file I get the same error
New folder structure:
ROOT/
libs/
__init__.py
file1.py
file2.py
function.py
logs/
debug.log
__init__.py
main.py
If I use this code in the libs/file1.py file, it works correctly (only if I start it from the main.py file)
# file1.py
from libs.function import function_name
But if in libs/file2.py I want to recall a variable present in the main.py file, it returns me an error
# file2.py
from main import data
# ERROR
No module named 'main'
If he doesn't give me this error he gives me another one
attempted relative import with no known parent package
I think your PYTHONPATH isn't set correctly. You can either export it or run your program with it set as expected for the single command. The following will set the PYTHONPATH for the single command execution:
PYTHONPATH=./:$PYTHONPATH python resource/console_log/file1.py
I saw a lot of questions about importing modules error but I could not manage to solve the problem with Gitlab CI pipeline.
My project structure:
├───config
├───docs
├───src
__init__.py
│ ├───dataset
__init__.py
│ ├───exceptions
│ ├───resources
│ └───utils
__init__.py
└───tests
__init__.py
└───resources
__init__.py
I would like to run tests using pytest.
I invoke this command python -m pytest -p no:cacheprovider or using unittest
'python -m unittest discover -v' from root directory and also tried to invoke from test directory. In both cases I have a problem with importing class from dataset module. What's interesting, I have two tests file.
First file imports:
import os import unittest
from src.utils.FileManager import FileManager
Second imports:
from src.dataset.DatasetHelper import DatasetHelper
The first file is passing but the second is failing with error:
from dataset import DatasetHelper ModuleNotFoundError: No module
named 'dataset'
So the thing is that other modules like utils from src are imported correctly, only the dataset has a problem. I am struggling with this a few days and I completely out of ideas. I also tried to change instead of from dataset to from src.dataset. It didn't work. I can run tests in PyCharm by clicking right mous button and just run tests but not on CI environment.
What I tried:
Add modules to $PYTHONPATH like
sys.path.insert(0, "/builds/USER/PROJECT/src/dataset")
sys.path.insert(0, "/builds/USER/PROJECT/src")
sys.path.insert(0, "/builds//USER/PROJECT/tests")
The content of PYTHONPATH before adding it was:
Current $PYTHONPATH: ['/builds/USER/PROJECT/config', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
The first module in list is config because I run script from this directory to add above modules to path. Doesn't Help
Run test command from root directory and add prefix src to imports in tests directory. Doesn't Help
from dataset import DatasetHelper
ModuleNotFoundError: No module named 'dataset'
Either in src.__init__ or more proabably in src.dataset.__init__ there is the import statement from dataset import DatasetHelper. You have to rewrite it as from src.dataset import…
You can try using a relative import in your __init__.py file that's inside the tests directory.
The syntax depends on the current location as well as the current location of the module,package, or object you're trying to import. Here are some examples:
from .some_module import some_class
from ..some_package import some_function
from . import some_class
Source: https://realpython.com/absolute-vs-relative-python-imports/
I am playing around with the AIMA python project, but I'm having trouble with importing the logic.py file into main.py. The following is folder structure:
project/
aima/
__init__.py
utils.py
logic.py
main.py
I added the folder to my python path variable. Every time I do
# main.py
import aima.logic as logic
I get this error:
File "main.py", line 2, in
import aima.logic as logic
File "/project/aima/logic.py", line 34, in
from utils import (
ImportError: No module named 'utils'
I thought this was strange since logic.py imports the utils file it should be fine since they are under the same directory.
I tried searching for answers, but most of them mention adding to python module search path and adding __init__.py and do not work for me.
Trying this may be good
from project.aima import logic
I have a parent package that has 2 child packages. It looks like this
backend
__init__.py
conf.py
db.py
connections.py
/api
__init__.py
register.py
api.py
/scheduled
__init__.py
helpers.py
All the __init__.py files are empty.
The code in backend/connections.py and backend/conf.py is being used by modules in both packages api and scheduled.
in register.py i have code like
from backend.conf import *
from backend.connections import *
Now when i do python register.py
i get this error
ImportError: No module named backend.conf
Also when i changed from backend.conf import * to from ..conf import * or from .. import conf i get this error
ValueError: Attempted relative import in non-package
What i understand by the above error is that python is not treating the above folders as packages. But i have __init__.py in all the folders. What is wrong?
When you run python register.py, your backend/register.py file is used as the __main__ module of the program, rather than as a module within the backend package. Further more, the Python import path will not automatically include the directory containing the backend directory, which is probably the cause of your problems.
One option that might work is to run your program as python -m backend.register from the top level directory of your project (or set PYTHONPATH so this module can be found). This will search for the script on the normal import path, and then run it as the main program.