This is my folder structure
src
├── __init__.py
└── foo
├── __init__.py
└── bar
├── __init__.py
├── events
│ ├── __init__.py
│ ├── execute
│ │ ├── __init__.py
│ │ ├── helloworld.py
│ │ ├── run.py
└── settings.py
$ cat src/foo/__init__.py outputs...
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
in src/foo/bar/events/execute/run.py, I want to do something like this
from foo.bar.events.execute.helloworld import HelloWorld
I get the error
No module named 'foo.bar'
This is how I'm running my app
I understand it's not proper, but there's a reason I just simplified the question for brevity
$ python src/foo/bar/events/execute/run
How do I achieve importing this way from src/foo/bar/events/execute/run.py?
from foo.bar.events.execute.helloworld import HelloWorld
Related
I am trying to turn a project of mine into a package so I can deploy it as a wheel.
I have a project directory setup like this:
ProjectDir
├── setup.py
├── MyModule
│ ├── __init__.py
│ ├── Module1
│ │ ├── __init__.py
│ │ ├── main.py
│ ├── Module2
│ │ ├── file1.py
│ │ ├── __init__.py
│ │ ├── file2.py
│ │ ├── file3.py
│ └── Module3
│ ├── __init__.py
│ ├── Sub1
│ │ ├── file1.py
│ │ ├── file2.py
│ │ ├── main.py
│ └── Sub2
│ ├── file1.py
│ ├── main.py
└── test
├── test_Module_1
│ ├── __init__.py
│ └── test_main.py
├── test_Module_2
...
Top level __init__.py is empty
Module 1 __init__.py file
from main import Function1
Similar for other module __init__.py files
setup.py
from setuptools import setup, find_packages
import os
import pip
setup(name='MyModule',
description='Tool suite to run MyModule',
packages=['MyModule'])
I can import MyModule but when I attempt to access any submodule, I get the following
AttributeError: module 'MyModule' has no attribute 'Module1'
Or if I check attributes of my module, none are found.
import MyModule
dir(MyModule)
['builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', 'os']
This is expected because by default submodules are not imported.
You should import them like so to use them:
import MyModule.Module1
To change this you have to tweak the MyModule/__init__.py file by adding:
import MyModule.Module1
This way, MyModule.Module1 will be available when you import MyModule, as the __init__.py file is executed.
I am very new to Python and I have the following structure for a project:
server/
├── config/
│ ├── __init__.py
│ ├── application.py
│ ├── dev.py
│ └── qa.py
├── lib/
│ ├── __init__.py
│ ├── redisdb.py
│ ├── logger.py
│ └── geo.py
└── scripts/
├── __init__.py
├── my_first_script.py
└── my_second_script.py
and in my_first_script.py file, I have the following code:
import pickle
from lib.redisdb import r
import re
import config.application as appconf
print( appconf.DOCUMENT_ENDPOINT )
partnerData = pickle.loads(r.get("partner_date_all"))
print( len(partnerData) )
When I run this code in the terminal using the command
python server/scripts/my_first_script.py
I am getting the following error:
Traceback (most recent call last):
File "my_first_script.py", line 3, in <module>
from lib.redisdb import r
ImportError: No module named lib.redisdb
I am using Python 2.7 version here. When I checked with Python 3 also, I have the same error. Now how can I execute this file? If my code doesn't have imports from the other local modules, everything works just fine.
Your modules are all siblings and you didn't not declare a parent package.
You could modify your structure this way so your modules can know each other.
server/ (assuming this is your project root)
server/ (assuming you want to call your package "server")
├── __init__.py
├── server.py (your package entry point)
├── config/
│ ├── __init__.py
│ ├── application.py
│ ├── dev.py
│ └── qa.py
├── lib/
│ ├── __init__.py
│ ├── redisdb.py
│ ├── logger.py
│ └── geo.py
└── scripts/
├── __init__.py
├── my_first_script.py
└── my_second_script.py
And now your imports can refer to the parent package:
for example:
# my_first_script.py
from server.config import application
python import from different level directory getting import error
Directory structure.
# all __init__.py files are empty
import/
├── helpers
│ ├── __init__.py
│ ├── helper1
│ │ ├── __init__.py
│ │ ├── helper1_module1.py
│ │ └── helper1_module2.py
│ └── helper2
│ ├── __init__.py
│ ├── helper2_module1.py
│ └── helper2_module2.py
└── services
├── __init__.py
├── service1
│ ├── __init__.py
│ └── service1_api.py
└── service2
helper1_module1.py
class Helper1Module1():
def function1(self):
print("executed from Helper1Module1 Class function1")
def function2(self):
print("executed from Helper1Module1 Class function2")
service1_api.py
from helpers.helper1.helper1_module1 import Helper1Module1
h = Helper1Module1()
h.function1()
Error:
python3 services/service1/service1_api.py
Traceback (most recent call last):
File "services/service1/service1_api.py", line 1, in <module>
from helpers.helper1.helper1_module1 import Helper1Module1
ModuleNotFoundError: No module named 'helpers'
How to fix this?
Python: python3.6 and above
OS: Linux
You have to set the file path (PYTHONPATH) manually to use the files from other directories.
You can export Environment Variable like
export PYTHONPATH=’path/to/directory’
Or you can use sys module: Importing files from different folder
I've been having troubles with this and cannot find the solution... I've browsed all SO questions about this but nothing worked in my case... So here's the problem:
I clone a project from git repository
Set up a virtualenv called env in project base dir with proper Python version(2.7) & install all reqs succesfully
Here's where it gets fun... I navigate to the folder that has my manage.py and do a python manage.py makemigrations which results in
from foo.bar.data import CoolioClass
ImportError: No module named foo.bar.data
My directories look like this (just for illustration):
project_root/
├──env/
├──django/
│ ├── app
│ │ └── models.py (from foo.bar.data import CoolioClass)
│ ├── django
│ │ └── settings.py
│ └── manage.py
└──foo/
├── bar
│ ├── __init__.py
│ ├── data.py
│ └── test.py
├── baz
│ ├── __init__.py
│ ├── data.py
│ └── test.py
└── __init__.py
When I print sys.path in python shell it results in:
/home/johnny/project_root/env/lib/python2.7
/home/johnny/project_root/env/lib/python2.7/plat-x86_64-linux-gnu
/home/johnny/project_root/env/lib/python2.7/lib-tk
/home/johnny/project_root/env/lib/python2.7/lib-old
/home/johnny/project_root/env/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/home/johnny/project_root/env/local/lib/python2.7/site-packages
/home/johnny/project_root/env/lib/python2.7/site-packages
I have the following directory structure in my Python3 project:
├── README.md
├── requirements.txt
├── schemas
│ ├── collector.sql
│ └── controller.sql
├── src
│ ├── controller.db
│ ├── controller.py
│ ├── measurement_agent.py
├── tests
│ ├── regression.py
│ ├── test-invalid-mac.py
│ ├── test-invalid-url.py
│ ├── test-register-ma-json.py
│ ├── test-register-ma-return-code.py
│ ├── test-send-capabilities-return-code.py
│ ├── test-valid-mac.py
│ └── test-valid-url.py
└── todo
In my tests folder I have some regression tests which are ran to check the consistency of the code from src/measurement_agent.py. The problem now is that I do not want to add to my path manually the measurement_agent.py to make an import from it. I would want to know if there is any trick how to tell Python to look in my root for the import I am trying to use.
Currently I am doing:
import os.path
ma = os.path.abspath('..') + '/src'
sys.path.append(ma)
from measurement_agent import check_hardware_address
and would want to have something just like
from measurement_agent import check_hardware_address
without using any os.path tricks.
Any suggestions?
Relative imports
Make sure there is an __init__.py in all folders including the top-most (the parent)
Use a relative import, like this:
from ..src import measurement_agent
Now to run your code, cd up to the parent of your parent directory and then
python -m parent.test.regression