2

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?

asked Dec 23, 2013 at 20:05
3
  • 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/ Commented Dec 23, 2013 at 20:10
  • In ServerHandle.py if I do "from ..Shared import MathProcessor" I get "Attempted relative import in non-package" Commented Dec 23, 2013 at 20:13
  • That's because none of your directories are packages. Commented Dec 23, 2013 at 20:16

1 Answer 1

4

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

answered Dec 23, 2013 at 20:12
Sign up to request clarification or add additional context in comments.

3 Comments

How come some of the questions here say to add it to the PATH is a terrible idea?
It depends on how and where you're doing it. Manipulating 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.
In my case it was usually due to not making test sub folders modules (ie with an init.py) file in so that Python could work out the references.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.