Import from relative path in Python

I have a folder for my client code, a folder for my server code and a folder for shared code between them

Proj/ Client/ Client.py Server/ Server.py Common/ __init__.py Common.py 

How do I import Common.py from Server.py and Client.py?

+50
python import relative-path
Sep 21 '11 at 20:09
source share
5 answers

Do not perform relative imports.

From PEP8 :

Relative imports for intra-batch imports are highly discouraged.

Put all your code in one super-package (ie, "myapp") and use subpackages for the client, server, and common code.

Update: "Python 2.6 and 3.x support proper relative imports (...)." See Dave for more details.

+10
Sep 21 '11 at 20:11
source share

EDIT Nov 2014 (3 years later):

Python 2.6 and 3.x support proper relative imports, where you can avoid hacking. With this method, you know that you are getting relative imports, not absolute imports. ".." means go to the directory above me:

 from ..Common import Common 

As a caution, this will only work if you run your python as a module, outside of it. For example:

 python -m Proj 

The original hacker way

This method is still often used in some situations where you never actually install your package. For example, it is popular among Django users.

You can add Common / to your sys.path (a list of paths to which python is trying 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 where your current python file is located, and then we go to the "Common /" directory and import the "Common" module.

+73
Sep 21 '11 at 20:13
source share

The default import method is already "relative", from PYTHONPATH. By default, PYTHONPATH is used for some system libraries along with the folder of the source source file. If you start with -m to start the module, the current directory is added to PYTHONPATH. Therefore, if the entry point of your program is inside Proj, then using import Common.Common should work both inside Server.py and Client.py.

Do not perform relative imports. It will not work the way you want.

+2
Sep 21 '11 at 20:13
source share

Performing relative imports is absolutely normal! Here's what I don't like:

 #first change the cwd to the script path scriptPath = os.path.realpath(os.path.dirname(sys.argv[0])) os.chdir(scriptPath) #append the relative location you want to import from sys.path.append("../common") #import your module stored in '../common' import common.py 
+2
Jan 06 '14 at 21:12
source share

Funny, the same problem that I just met, and I get this work as follows:

combined with the linux ln command, we can make a lot more prettier:

 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 a 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 thing applies to Proj/Server , the setup.py issue also works for the setup.py process that was discussed here , hope this helps!

+2
Apr 17 '15 at 7:03
source share



All Articles