I have a package directory structure as follows
root-
|
Common (contains __init__.py file)
WindowsComponents (contains __init__.py file)
...
I would like modules in the WindowsComponents directory to be able to
import some modules from the Common directory. In my first pass, I was
able to append sys.path ( sys.path.append('../Common') ) in each module
that wants to import from Common, but this feels "clunky". Is there a
"standard"/"best" way to accomplish this?
--ERick 10 1350
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?
import os, sys
sys.path.append(os.path.join(os.getcwd(), 'Common'))
--ERick
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?
import os, sys
sys.path.append(os.path.join(os.getcwd(), 'Common'))
--ERick
On Nov 16, er**********@comcast.net wrote: I have a package directory structure as follows
root- | Common (contains __init__.py file) WindowsComponents (contains __init__.py file) ...
I would like modules in the WindowsComponents directory to be able to import some modules from the Common directory.
So you now have a "Common" package. And it might contain a "mustard"
module.
In my first pass, I was able to append sys.path ( sys.path.append('../Common') ) in each module that wants to import from Common, but this feels "clunky".
Agreed. You probably want to avoid messing with sys.path whenever
possible.
Is there a "standard"/"best" way to accomplish this?
So "root" should already be on your sys.path/PYTHONPATH.
Then in say file "root/WindowsComponents/spam.py":
from Common import mustard
...
mustard.attr
More import info from Fredrik: http://effbot.org/zone/import-confusion.htm
--
_ _ ___
|V|icah |- lliott <>< md*@micah.elliott.name
" " """
On Nov 16, er**********@comcast.net wrote: I have a package directory structure as follows
root- | Common (contains __init__.py file) WindowsComponents (contains __init__.py file) ...
I would like modules in the WindowsComponents directory to be able to import some modules from the Common directory.
So you now have a "Common" package. And it might contain a "mustard"
module.
In my first pass, I was able to append sys.path ( sys.path.append('../Common') ) in each module that wants to import from Common, but this feels "clunky".
Agreed. You probably want to avoid messing with sys.path whenever
possible.
Is there a "standard"/"best" way to accomplish this?
So "root" should already be on your sys.path/PYTHONPATH.
Then in say file "root/WindowsComponents/spam.py":
from Common import mustard
...
mustard.attr
More import info from Fredrik: http://effbot.org/zone/import-confusion.htm
--
_ _ ___
|V|icah |- lliott <>< md*@micah.elliott.name
" " """
erick_bod...@comcast.net wrote: I think I have an answer to my own question. In the WindowsComponents/__init__.py file, I have the following, that feels like a better answer for the problem. Is there a better answer than this?
import os, sys sys.path.append(os.path.join(os.getcwd(), 'Common'))
My solution to this is to use a .pth file. In my site-packages folder
under the python installation, I have a file named infidel.pth. This
file contains the path to the folder where I put all my python source
code (C:\src\py). In your case, you could have the path to your 'root'
folder.
One of my projects is structured like this:
C:\src\py
infidel\
__init__.py
models\
__init__.py
basemodel.py
views\
__init__.py
baseview.py
controllers\
__init__.py
Now the controllers package can do imports like this:
from infidel.models import basemodel
from infidel.views import baseview
The point is that the .pth file in site-packages adds custom paths to
your sys.path
erick_bod...@comcast.net wrote: I think I have an answer to my own question. In the WindowsComponents/__init__.py file, I have the following, that feels like a better answer for the problem. Is there a better answer than this?
import os, sys sys.path.append(os.path.join(os.getcwd(), 'Common'))
My solution to this is to use a .pth file. In my site-packages folder
under the python installation, I have a file named infidel.pth. This
file contains the path to the folder where I put all my python source
code (C:\src\py). In your case, you could have the path to your 'root'
folder.
One of my projects is structured like this:
C:\src\py
infidel\
__init__.py
models\
__init__.py
basemodel.py
views\
__init__.py
baseview.py
controllers\
__init__.py
Now the controllers package can do imports like this:
from infidel.models import basemodel
from infidel.views import baseview
The point is that the .pth file in site-packages adds custom paths to
your sys.path er**********@comcast.net a écrit : I think I have an answer to my own question. In the WindowsComponents/__init__.py file, I have the following, that feels like a better answer for the problem. Is there a better answer than this?
import os, sys sys.path.append(os.path.join(os.getcwd(), 'Common'))
Err... if the script is called from somewhere else, this won't work.
replace os.getcwd() with os.path.dirname(os.path.realpath(__file__))
Or better, use a .pth or add the needed path in your PYTHONPATH er**********@comcast.net a écrit : I think I have an answer to my own question. In the WindowsComponents/__init__.py file, I have the following, that feels like a better answer for the problem. Is there a better answer than this?
import os, sys sys.path.append(os.path.join(os.getcwd(), 'Common'))
Err... if the script is called from somewhere else, this won't work.
replace os.getcwd() with os.path.dirname(os.path.realpath(__file__))
Or better, use a .pth or add the needed path in your PYTHONPATH
Bruno Desthuilliers wrote: er**********@comcast.net a écrit : I think I have an answer to my own question. In the WindowsComponents/__init__.py file, I have the following, that feels like a better answer for the problem. Is there a better answer than this?
import os, sys sys.path.append(os.path.join(os.getcwd(), 'Common'))
Err... if the script is called from somewhere else, this won't work. replace os.getcwd() with os.path.dirname(os.path.realpath(__file__))
Right, I anticipate the script(s) won't be called from elsewhere,
but.... Or better, use a .pth or add the needed path in your PYTHONPATH
THis would be ideal if I could gaurantee that the users (other software
testers) would install the *.pth file.
thanks for all the suggestions.
Bruno Desthuilliers wrote: er**********@comcast.net a écrit : I think I have an answer to my own question. In the WindowsComponents/__init__.py file, I have the following, that feels like a better answer for the problem. Is there a better answer than this?
import os, sys sys.path.append(os.path.join(os.getcwd(), 'Common'))
Err... if the script is called from somewhere else, this won't work. replace os.getcwd() with os.path.dirname(os.path.realpath(__file__))
Right, I anticipate the script(s) won't be called from elsewhere,
but.... Or better, use a .pth or add the needed path in your PYTHONPATH
THis would be ideal if I could gaurantee that the users (other software
testers) would install the *.pth file.
thanks for all the suggestions. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David M. Wilson |
last post by:
Hello!
I maintain a small package for talking to the API of BulkSMS.co.uk. I
have been adding support for some new features recently, and found
myself slightly indecisive over how best to lay...
|
by: Jennifer |
last post by:
I've created a DTS package and now I need to distribute it to
different servers.
I've been looking for a way to automatically/programatically create a
DTS package, but have not found anything...
|
by: jcooper |
last post by:
Hi all,
I'm new to VB.net and have a question about creating the .exe file for my
program. In VB6 you could just select 'Make .exe' from the File menu, but
everything that I read about...
|
by: jcooper |
last post by:
Hi all,
I'm new to VB.net and have a question about creating the .exe file for my
program. In VB6 you could just select 'Make .exe' from the File menu, but
everything that I read about...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
| |