python


Python: How to add module from sibling folder


I have the following folder structure for my Python Project:
pythonApp --> Folder
|--ABC --> Package
|--__init__.py --> Empty File
|--abctest.py --> Module
|--DEF --> Package
|--__init__.py --> Empty File
|--deftest.py --> Module
|--Common --> Package
|--__init__.py --> Empty File
|--constants.py --> Module
I want to import constants.pyunder the Common package in the abctest.py. Using from Common import constants throws error Module not found. Is there any solution for this.
in abctest.py, add the parent directory to python path using __file__ as current module name, then take the dirname of the dirname to compute it:
import os,sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from Common import constants
note: python 2 seems to need a __init__.py file (even empty) in Common directory to be able to recognize Common as a module, whereas python 3 can do without.

Related Links

Using user input to access a variable in python
String replacement in excel using Python
Read a File and Count Characters from an inputted txt file from user
OpenCV camera just showing black output
Why am I getting an empty row in my dataframe after using pandas apply?
List circulation in Python for Project Euler 37
Python lists, pairing and writting
searching sickbeard api output and print only keyword line
Flask-Restless API Endpoints only work sometimes
Matplotlib tight_layout causing RuntimeError
Strange behaviour of ref counter with ints being 0
self.assertTrue(post.text in response.content) – Assertion Error
How to add a path to sys.path permanently [duplicate]
Copy C structure to Python List
python error:List index out of range
While loop inside for loop?

Categories

HOME
sql-server-2008
laravel-5
security
cluster-computing
symfony
ibm-watson-cognitive
sass
openssl
angular-cli
webrtc
value
jrebel
gspread
ext.net
docker-windows
websphere-liberty
goutte
desktop
iso
xul
event-log
netezza
cross-platform
r-lavaan
off-canvas-menu
tortoisegit
multiple-columns
jconsole
delete-file
kong
bus-error
pptp
web-frontend
mayavi
pcre
protobuf-net
spring-profiles
edb
hawtio
rhino
honeysql
google-guava-cache
jboss-esb
forecasting
installshield-2012
bayesian-networks
stereo-3d
filepath
prototypejs
ajp
android-bitmap
pagefile
addin-express
laravel-query-builder
createobject
gradle-script-kotlin
typhoon
c#-interactive
debugdiag
audioqueue
aescryptoserviceprovider
tomcat5
multiple-file-upload
brython
inotifypropertychanged
scriptlet
logcat
flexjson
magento-1.4
license-key
jenkins-scriptler
unity5.3
js-cookie
visual-studio-code
castle-windsor-3
viper-architecture
sparse-file
pgm
shift-jis
custom-url
word-2013
inputaccessoryview
tilestache
named-ranges
kendo-mvvm
ibmsbt
valence
phpdocx
codeigniter-a3m
help-viewer
lov
visual-studio-addins
angulartics
java.util.date
fogbugz-api
robospice
driver-signing
soa-suite
fraud-prevention
icsharpcode
transitive-closure-table
static-variables
tfs-power-tools
parameterization
responsetext
visual-web-gui
selectmanycheckbox
nemerle
surf
scraperwiki
executable-format
opengl-es-lighting
revert
scatterview
soft-keyboard
hardware-infrastructure

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App