I am having some trouble understanding how to manage this code for my project. Due to how imports work in python, it's hard for me to have ONE git repo for all of my classes.
The directory layout is like this :
(ASSIGNMENT 3 GIT REPO)
Project/
Client/
Main.py
ClientHandler.py
ClientSoundManager.py
Server/
Main.py
ServerHandle.py
ServerUtil.py
Shared/
MathProcessor.py
DrawHandler.py
SoundProcessor.py
I have one git project -- "Assignment 3." In python I can't import MathProcessor in ServerHandle.py, and same for ClientHandler.py. My other option is to create separate repositories for each... and make it look like this --
(ASSIGNMENT 3 GIT REPO)
Project/
Client/
Shared/ (SHARED GIT REPO)
MathProcessor.py
DrawHandler.py
SoundProcessor.py
Main.py
CLientHandler.py
ClientSoundManager.py
Server/
Shared/ (SHARED GIT REPO)
MathProcessor.py
DrawHandler.py
SoundProcessor.py
Main.py
ServerHandle.py
ServerUtil.py
If I do that, then I'll have a GIT repo inside of a GIT repo... What is the proper way to manage this project so python imports don't have to be hacked in?
-
In python I can't import MathProcessor in ServerHandle.py, and same for ClientHandler.py - why is that? What error are you getting? How are you doing the import/MattDMo– MattDMo2013年12月23日 20:10:06 +00:00Commented Dec 23, 2013 at 20:10
-
In ServerHandle.py if I do "from ..Shared import MathProcessor" I get "Attempted relative import in non-package"user791953– user7919532013年12月23日 20:13:37 +00:00Commented Dec 23, 2013 at 20:13
-
That's because none of your directories are packages.bruno desthuilliers– bruno desthuilliers2013年12月23日 20:16:16 +00:00Commented Dec 23, 2013 at 20:16
1 Answer 1
This has nothing to do with git. You just need to have /path/to/wherever/you/cloned/Project your sys.path AND add the necessaries __init__.py files in Project/Client, Project/Server and Project/Shared to make the Python packages.
You'll find most relevant infos about imports, sys.path and packages here : http://docs.python.org/2/tutorial/modules.html
3 Comments
sys.path in library code is more often than not a bad idea. It's sometimes necessary in in some scripts (ie in a mod_wsgi script when using virtualenv). Adding to your PYTHON_PATH env variable is usually ok.