PYTHONPATH: dev and prod

J

jacopo

I am developing my code in the path:
/py/myscripts
/py/mylib
In order to "import mylib", I need to add /py/mylib to PYTHONPATH.

Now I want to save a snapshot of the current code in the production directory, I will copy all in:
/prod/myscripts
/prod/mylib

The problem now is that when I execute /prod/myscripts/any_script.py, every "import" will look at PYTHONPATH and therefore it will load the modules from /py/mylib. On the contrary I want to load it from /prod/mylib.

Is there an elegant way to cope with this?

thanks, Jacopo
 
R

rusi

I am developing my code in the path:
/py/myscripts
/py/mylib
In order to "import mylib", I need to add /py/mylib to PYTHONPATH.

Now I want to save a snapshot of the current code in the production directory, I will copy all in:
/prod/myscripts
/prod/mylib

The problem now is that when I execute /prod/myscripts/any_script.py, every "import" will look at PYTHONPATH and therefore it will load the modules from /py/mylib. On the contrary I want to load it from /prod/mylib.

Is there an elegant way to cope with this?

thanks, Jacopo

<I-think>
Use explicit (dot-based) relative imports
http://docs.python.org/release/2.5/whatsnew/pep-328.html
Avoid using PYTHONPATH
</I-think>
 
J

jacopo

this idea seemed perfect but it turned out that you have to execute the module as a package (python -m scripts.myscript) otherwise I get an error on the relative import.
Unfortunately I am working in a team and I do not have control on how the module is launched.
 
J

jacopo

this idea seemed perfect but it turned out that you have to execute the module as a package (python -m py.myscripts.any_script) otherwise I get an error on the relative import.
Unfortunately I am working in a team and I do not have control on how the module is launched.
 
R

rusi

this idea seemed perfect but it turned out that you have to execute the module as a package
(python -m py.myscripts.any_script) otherwise I get an error on the relative import.
Unfortunately I am working in a team and I do not have control on how themodule is launched.

You need to give more data:

1. How you run -- 'launch' -- the code -- from py and from prod
2. What error you get
3. Did you try bundling your modules into a package? What problem
happened?

If PYTHONPATH does not work for you you can look at path-configuration-
files (.pth) files
http://docs.python.org/2/library/site.html [Ive not used them myself]
 
J

jacopo

1. How you run -- 'launch' -- the code -- from py and from prod
when I have to test I use "python any_script.py" but in production there is a c++ program that is able to wrap and run python code (the technical details are a bit beyond my knowledge)
2. What error you get
when I run as "python any_script.py" I get
"ValueError: Attempted relative import in non-package"
I have found this explanation:
http://stackoverflow.com/questions/...ative-import-in-non-package-even-with-init-py
So I have added at the top of my module:

if __name__=="__main__" and __package__ is None:
__package__="myscripts"
from ..mylib import MyClass

and I get the error:
SystemError: Parent module 'mylib' not loaded, canno perform relative import

3. Did you try bundling your modules into a package? What problem
happened?
What does it mean? what does build mean in this contest? I am not compiling, I just write the .py files.
 
R

rusi

when I have to test I use "python any_script.py"  but in production there is a c++ program that is able to wrap and run python code (the technicaldetails are a bit beyond my knowledge)


when I run as "python any_script.py" I get
"ValueError: Attempted relative import in non-package"
I have found this explanation:http://stackoverflow.com/questions/11536764/attempted-relative-import...
So I have added at the top of my module:

if __name__=="__main__" and __package__ is None:
     __package__="myscripts"
from ..mylib import MyClass

and I get the error:
SystemError: Parent module 'mylib' not loaded, canno perform relative import


What does it mean? what does build mean in this contest? I am not compiling, I just write the .py files.

I mean use a python package to wrap the modules.
Roughly that means have a file named __init__.py in the root of your
modules directory.
For the details see http://docs.python.org/2/tutorial/modules.html#packages

More intricacies I dont know. Hopefully someone who knows better can
answer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top