Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

added 214 characters in body
Source Link
Lorem Ipsum
  • 4.6k
  • 4
  • 51
  • 82

Python paths can get kind of tricky. I don't claim that this is the best approach, but it's what I hacked together on my lunch. Perhaps it can get you where you need to go.

When you run main.py, you're in the internal/ directory. You need to go up a level and then access the user/ directory. That's what happens with sys.path.append('../'). The module usermanager.py is in the user/ directory, so to import from user.py, we want to point to the current directory. Hence the period in from .user import User.

Here's the directory structure I used. I believe it is the same as what you have.

C:.
├───internal
│ main.py
│ __init__.py
│
└───user
 │ models.py
 │ user.py
 │ usermanager.py
 └───__init__.py

_init___init__.py

The _init_.py__init__.py files are empty. Their presence indicates to Python that the directories are modules1.

main.py

import sys
sys.path.append('../')
from user import user
from user import usermanager
from user import models
my_user = user.User()
my_user.message()
my_user_manager = usermanager.UserManager()
my_user_manager.message()
my_user_manager.CreateUser()
my_model = models.Model()
my_model.message()

models.py

class Model():
 def message(self):
 print("This is a Model!")

user.py

class User():
 def message(self):
 print("This is a User!")

usermanager.py

from .user import User
class UserManager():
 
 def message(self):
 print("This is a UserManager!")
 def CreateUser(self):
 new_user = User()
 print("I created the user: ", new_user)

Now, when I call main.py I get:

c:\project\internal>python main.py
This is a User!
This is a UserManager!
I created the user: <user.user.User object at 0x0000000002A61EB8>
This is a Model!

Everyone is talking to everyone else. Hopefully you can extrapolate that to your use case! If not, Chris Yeh has an article "The Definitive Guide to Python import Statements" you may find helpful.


1 See What is __init__.py for? to learn more about how that works.

Python paths can get kind of tricky. I don't claim that this is the best approach, but it's what I hacked together on my lunch. Perhaps it can get you where you need to go.

When you run main.py, you're in the internal/ directory. You need to go up a level and then access the user/ directory. That's what happens with sys.path.append('../'). The module usermanager.py is in the user/ directory, so to import from user.py, we want to point to the current directory. Hence the period in from .user import User.

Here's the directory structure I used. I believe it is the same as what you have.

C:.
├───internal
│ main.py
│ __init__.py
│
└───user
 │ models.py
 │ user.py
 │ usermanager.py
 └───__init__.py

_init_.py

The _init_.py files are empty.

main.py

import sys
sys.path.append('../')
from user import user
from user import usermanager
from user import models
my_user = user.User()
my_user.message()
my_user_manager = usermanager.UserManager()
my_user_manager.message()
my_user_manager.CreateUser()
my_model = models.Model()
my_model.message()

models.py

class Model():
 def message(self):
 print("This is a Model!")

user.py

class User():
 def message(self):
 print("This is a User!")

usermanager.py

from .user import User
class UserManager():
 
 def message(self):
 print("This is a UserManager!")
 def CreateUser(self):
 new_user = User()
 print("I created the user: ", new_user)

Now, when I call main.py I get:

c:\project\internal>python main.py
This is a User!
This is a UserManager!
I created the user: <user.user.User object at 0x0000000002A61EB8>
This is a Model!

Everyone is talking to everyone else. Hopefully you can extrapolate that to your use case! If not, Chris Yeh has an article "The Definitive Guide to Python import Statements" you may find helpful.

Python paths can get kind of tricky. I don't claim that this is the best approach, but it's what I hacked together on my lunch. Perhaps it can get you where you need to go.

When you run main.py, you're in the internal/ directory. You need to go up a level and then access the user/ directory. That's what happens with sys.path.append('../'). The module usermanager.py is in the user/ directory, so to import from user.py, we want to point to the current directory. Hence the period in from .user import User.

Here's the directory structure I used. I believe it is the same as what you have.

C:.
├───internal
│ main.py
│ __init__.py
│
└───user
 │ models.py
 │ user.py
 │ usermanager.py
 └───__init__.py

__init__.py

The __init__.py files are empty. Their presence indicates to Python that the directories are modules1.

main.py

import sys
sys.path.append('../')
from user import user
from user import usermanager
from user import models
my_user = user.User()
my_user.message()
my_user_manager = usermanager.UserManager()
my_user_manager.message()
my_user_manager.CreateUser()
my_model = models.Model()
my_model.message()

models.py

class Model():
 def message(self):
 print("This is a Model!")

user.py

class User():
 def message(self):
 print("This is a User!")

usermanager.py

from .user import User
class UserManager():
 
 def message(self):
 print("This is a UserManager!")
 def CreateUser(self):
 new_user = User()
 print("I created the user: ", new_user)

Now, when I call main.py I get:

c:\project\internal>python main.py
This is a User!
This is a UserManager!
I created the user: <user.user.User object at 0x0000000002A61EB8>
This is a Model!

Everyone is talking to everyone else. Hopefully you can extrapolate that to your use case! If not, Chris Yeh has an article "The Definitive Guide to Python import Statements" you may find helpful.


1 See What is __init__.py for? to learn more about how that works.

edited body
Source Link
Lorem Ipsum
  • 4.6k
  • 4
  • 51
  • 82

Python paths can get kind of tricky. I don't claim that this is the best approach, but it's what I hacked together on my lunch. Perhaps it can get you where you need to go.

When you run main.py, you're in the internal/ directory. You need to go up a level and then access the user/ directory. That's thatwhat happens with sys.path.append('../'). The module usermanager.py is in the user/ directory, so to import from user.py, we want to point to the current directory. Hence the period in from .user import User.

Here's the directory structure I used. I believe it is the same as what you have.

C:.
├───internal
│ main.py
│ __init__.py
│
└───user
 │ models.py
 │ user.py
 │ usermanager.py
 └───__init__.py

_init_.py

The _init_.py files are empty.

main.py

import sys
sys.path.append('../')
from user import user
from user import usermanager
from user import models
my_user = user.User()
my_user.message()
my_user_manager = usermanager.UserManager()
my_user_manager.message()
my_user_manager.CreateUser()
my_model = models.Model()
my_model.message()

models.py

class Model():
 def message(self):
 print("This is a Model!")

user.py

class User():
 def message(self):
 print("This is a User!")

usermanager.py

from .user import User
class UserManager():
 
 def message(self):
 print("This is a UserManager!")
 def CreateUser(self):
 new_user = User()
 print("I created the user: ", new_user)

Now, when I call main.py I get:

c:\project\internal>python main.py
This is a User!
This is a UserManager!
I created the user: <user.user.User object at 0x0000000002A61EB8>
This is a Model!

Everyone is talking to everyone else. Hopefully you can extrapolate that to your use case! If not, Chris Yeh has an article "The Definitive Guide to Python import Statements" you may find helpful.

Python paths can get kind of tricky. I don't claim that this is the best approach, but it's what I hacked together on my lunch. Perhaps it can get you where you need to go.

When you run main.py, you're in the internal/ directory. You need to go up a level and then access the user/ directory. That's that happens with sys.path.append('../'). The module usermanager.py is in the user/ directory, so to import from user.py, we want to point to the current directory. Hence the period in from .user import User.

Here's the directory structure I used. I believe it is the same as what you have.

C:.
├───internal
│ main.py
│ __init__.py
│
└───user
 │ models.py
 │ user.py
 │ usermanager.py
 └───__init__.py

_init_.py

The _init_.py files are empty.

main.py

import sys
sys.path.append('../')
from user import user
from user import usermanager
from user import models
my_user = user.User()
my_user.message()
my_user_manager = usermanager.UserManager()
my_user_manager.message()
my_user_manager.CreateUser()
my_model = models.Model()
my_model.message()

models.py

class Model():
 def message(self):
 print("This is a Model!")

user.py

class User():
 def message(self):
 print("This is a User!")

usermanager.py

from .user import User
class UserManager():
 
 def message(self):
 print("This is a UserManager!")
 def CreateUser(self):
 new_user = User()
 print("I created the user: ", new_user)

Now, when I call main.py I get:

c:\project\internal>python main.py
This is a User!
This is a UserManager!
I created the user: <user.user.User object at 0x0000000002A61EB8>
This is a Model!

Everyone is talking to everyone else. Hopefully you can extrapolate that to your use case! If not, Chris Yeh has an article "The Definitive Guide to Python import Statements" you may find helpful.

Python paths can get kind of tricky. I don't claim that this is the best approach, but it's what I hacked together on my lunch. Perhaps it can get you where you need to go.

When you run main.py, you're in the internal/ directory. You need to go up a level and then access the user/ directory. That's what happens with sys.path.append('../'). The module usermanager.py is in the user/ directory, so to import from user.py, we want to point to the current directory. Hence the period in from .user import User.

Here's the directory structure I used. I believe it is the same as what you have.

C:.
├───internal
│ main.py
│ __init__.py
│
└───user
 │ models.py
 │ user.py
 │ usermanager.py
 └───__init__.py

_init_.py

The _init_.py files are empty.

main.py

import sys
sys.path.append('../')
from user import user
from user import usermanager
from user import models
my_user = user.User()
my_user.message()
my_user_manager = usermanager.UserManager()
my_user_manager.message()
my_user_manager.CreateUser()
my_model = models.Model()
my_model.message()

models.py

class Model():
 def message(self):
 print("This is a Model!")

user.py

class User():
 def message(self):
 print("This is a User!")

usermanager.py

from .user import User
class UserManager():
 
 def message(self):
 print("This is a UserManager!")
 def CreateUser(self):
 new_user = User()
 print("I created the user: ", new_user)

Now, when I call main.py I get:

c:\project\internal>python main.py
This is a User!
This is a UserManager!
I created the user: <user.user.User object at 0x0000000002A61EB8>
This is a Model!

Everyone is talking to everyone else. Hopefully you can extrapolate that to your use case! If not, Chris Yeh has an article "The Definitive Guide to Python import Statements" you may find helpful.

Source Link
Lorem Ipsum
  • 4.6k
  • 4
  • 51
  • 82

Python paths can get kind of tricky. I don't claim that this is the best approach, but it's what I hacked together on my lunch. Perhaps it can get you where you need to go.

When you run main.py, you're in the internal/ directory. You need to go up a level and then access the user/ directory. That's that happens with sys.path.append('../'). The module usermanager.py is in the user/ directory, so to import from user.py, we want to point to the current directory. Hence the period in from .user import User.

Here's the directory structure I used. I believe it is the same as what you have.

C:.
├───internal
│ main.py
│ __init__.py
│
└───user
 │ models.py
 │ user.py
 │ usermanager.py
 └───__init__.py

_init_.py

The _init_.py files are empty.

main.py

import sys
sys.path.append('../')
from user import user
from user import usermanager
from user import models
my_user = user.User()
my_user.message()
my_user_manager = usermanager.UserManager()
my_user_manager.message()
my_user_manager.CreateUser()
my_model = models.Model()
my_model.message()

models.py

class Model():
 def message(self):
 print("This is a Model!")

user.py

class User():
 def message(self):
 print("This is a User!")

usermanager.py

from .user import User
class UserManager():
 
 def message(self):
 print("This is a UserManager!")
 def CreateUser(self):
 new_user = User()
 print("I created the user: ", new_user)

Now, when I call main.py I get:

c:\project\internal>python main.py
This is a User!
This is a UserManager!
I created the user: <user.user.User object at 0x0000000002A61EB8>
This is a Model!

Everyone is talking to everyone else. Hopefully you can extrapolate that to your use case! If not, Chris Yeh has an article "The Definitive Guide to Python import Statements" you may find helpful.

lang-py

AltStyle によって変換されたページ (->オリジナル) /