This is what I use:
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
Answer from jterrace on Stack OverflowThis is what I use:
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
I'm using:
import sys,os
sys.path.append(os.getcwd())
Appending paths relative to the current file to sys.path - Post.Byes
improving sys.path.append situation
Importing from a relative path in Python - Stack Overflow
Cannot import relative python paths
I have a structure like this:
Parent_Folder
-- foo.py
Child_Folder_1
-- bar.py
-- egg.py
Child_Folder_2
-- spam.pyI tried relative importing on bar.py,
from ..import foo
but it did not work for reasons, and I somehow found a way to do this via internet.
import sys
sys.path.append(".")now I can import foo.py and egg.py and even spam.py on bar.py, but I wonder how this is working, and what did that single line do. I've searched the web, but I doubt I found any understandable answers, so I'm posting this question here.
Since I'm not a professional at neither python nor programming, a kind explaination would be welcome :D Anyway, thanks for your time for reading this.
I see my code is breaking due to possible overuse of sys.path.append. I'm working with lots of repositories and building libraries for me and others, so I have to pass relative path in this function. I'm comming from javascript, what's best practice with this when it comes ot python? As far as came this was the best thread.
I asked this on Python reddit and got following answer from jwink3101:
But anyway, you have to be more clear. At the top of your script, you do not want to do:
sys.path.append('/path/to/dir') unless you will only ever run it once and non-interactively. Instead do:
if '/path/to/dir' not in sys.path: sys.path.append('/path/to/dir') But, really, you do not want to work this way. You should install what you need, make virtualenv's etc. (with that said, I do this too!)
################
I had to move it to this subreddit.
So in essence I have few of my private packages which I can't put on pip etc. and I'm tired of using sys.path.append because my path gets completely ruined. What's alternative in my case? For example, a lot of times I have to import ../someModule.py ,but isn't sys.path.append bad practice?
How python finds its modules
Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named __init__.py, to tell python it is a package to import modules from. In both cases, modules need their .py extension, but importing them is done without (see further below).
By default, Python looks for its modules and packages in $PYTHONPATH.
To find out what is included in $PYTHONPATH, run the following code in python (3):
import sys
print(sys.path)
How to add a directory
Occasionally
From within a python file, you can add path(s) occasionally to the default path by adding the following lines in the head section of your python application or script:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
For example:
if I have a folder: /home/myname/pythonfiles, and I want to import the file module_1.py, located in that directory, I add this to the head section of my code:
import sys
sys.path.insert(0, "/home/myname/pythonfiles")
And I can simply import the file module_1.py by:
import module_1
When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH, containing the modules, accompanied by a (can be empty) file called __init__.py
For example:
To import from a package (folder) called my_package in /home/myname/pythonfiles , add the /home/myname/pythonfiles path to your $PYTHONPATH, like in example 1, and import the module called module_2.py (inside the package folder) simply with: `
from <packagename> import module_2
Adding directories to $PYTHONPATH permanently:
Add the following line to your ~/.profile file.
export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add
Subdirectories
From within a package, subdirectories are not included just like that; you need to "chain" the directories. To import a module module_3.py, inside folder subfolder inside folder packagename:
import packagename.subfolder.module_3
Given the fact that all subfolders in the package include their own __init__.py file.
When a module is in the same directory as the script or application
There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.
Example:
If I have a folder, containing script.py and module.py, I can simply import the module by:
import module
The correct way to use relative import is:
from ..Common import foo1
And you also need a __init__.py in all your folders.
EDIT Nov 2014 (3 years later):
Python 2.6 and 3.x supports proper relative imports, where you can avoid doing anything hacky. With this method, you know you are getting a relative import rather than an absolute import. The '..' means, go to the directory above me:
from ..Common import Common
As a caveat, this will only work if you run your python as a module, from outside of the package. For example:
python -m Proj
Original hacky way
This method is still commonly used in some situations, where you aren't actually ever 'installing' your package. For example, it's popular with Django users.
You can add Common/ to your sys.path (the list of paths python looks at to import things):
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'Common'))
import Common
os.path.dirname(__file__) just gives you the directory that your current python file is in, and then we navigate to 'Common/' the directory and import 'Common' the module.
Funny enough, a same problem I just met, and I get this work in following way:
combining with linux command ln , we can make thing a lot simper:
1. cd Proj/Client
2. ln -s ../Common ./
3. cd Proj/Server
4. ln -s ../Common ./
And, now if you want to import some_stuff from file: Proj/Common/Common.py into your file: Proj/Client/Client.py, just like this:
# in Proj/Client/Client.py
from Common.Common import some_stuff
And, the same applies to Proj/Server, Also works for setup.py process,
a same question discussed here, hope it helps !