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

General rule about which Pandas operations on Series keep the name?
Mongoengine query set to list conversion
When using Python Windows Launcher, is there any way to prevent having to type full path?
map,lambda and append.. why doesn't it work?
Linux Nagios checks Issues: MongoDB check_mongodb.py
Django How would I filter Friend objects elegantly by name when using the built in User model?
How to use threading to get user input realtime while main still running in python
Why is python rounding this value?
Logging into a forum using Python Requests
Could bayesian network input data be probability?
Django report builder TEMPLATE_CONTEXT_PROCESSORS and Django 1.8
frames in a saved matplotlib animation are out of order
Error releasing memory for a pointer to a ctypes structure?
Dynamic Table with django form [closed]
Appending to a list from an iterated dictionary
A python script to be running on Django server

Categories

HOME
shopify
webrtc
bower
angular2-routing
windows-store-apps
codeeffects
chaiscript
heap-memory
sbt-assembly
codeblocks
shader
slim-3
diagram
vuex
google-shopping
finite-group-theory
qt-installer
gwtp
r-lavaan
python-unicode
session-timeout
thumbnails
clover
kadanes-algorithm
nsmenuitem
bootstrap-typeahead
rhmap
guile
lync-2013
version-numbering
ab-initio
frp
pygooglechart
protobuf-net
edb
pdflatex
getline
pace
wfp
dql
agent
lexical-analysis
code-composer
control-flow-graph
dojox.mobile
amd
encapsulation
context-free-language
lumen-5.3
double-buffering
goquery
google-maps-ios
crash-reports
forerunnerdb
scd
paas
communication-protocol
onresume
manifest.mf
picasa
activeweb
cron-task
unsatisfiedlinkerror
linkageerror
gemini
handlebars.java
git-rebase
asymptote
difference
apigee-baas
git-checkout
google-plus-one
hiera
virus
adodb
obfuscar
kendo-mvvm
emma
cppdepend
sgml
dbsetup
flash-cc
asp.net-mvc-scaffolding
ienumerator
fieldset
pys60
code-conversion
returnurl
icefaces-3
responsetext
flexicious
nssavepanel
tcxgrid
gtk2hs
scalaxb
ihttphandler
libavformat
chatroom
getresource
hibernate3-maven-plugin
quick-search
file-encodings
rtsp-client
act
economics

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