Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47673 > unrolled thread

PYTHONPATH: dev and prod

Started byjacopo <jacopo.pecci@gmail.com>
First post2013-06-11 09:28 -0700
Last post2013-06-13 03:52 -0700
Articles 8 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  PYTHONPATH: dev and prod jacopo <jacopo.pecci@gmail.com> - 2013-06-11 09:28 -0700
    Re: PYTHONPATH: dev and prod rusi <rustompmody@gmail.com> - 2013-06-11 10:14 -0700
      Re: PYTHONPATH: dev and prod jacopo <jacopo.pecci@gmail.com> - 2013-06-12 04:09 -0700
      Re: PYTHONPATH: dev and prod jacopo <jacopo.pecci@gmail.com> - 2013-06-12 04:10 -0700
        Re: PYTHONPATH: dev and prod rusi <rustompmody@gmail.com> - 2013-06-12 04:50 -0700
          Re: PYTHONPATH: dev and prod jacopo <jacopo.pecci@gmail.com> - 2013-06-12 06:29 -0700
            Re: PYTHONPATH: dev and prod rusi <rustompmody@gmail.com> - 2013-06-12 09:10 -0700
              Re: PYTHONPATH: dev and prod jacopo <jacopo.pecci@gmail.com> - 2013-06-13 03:52 -0700

#47673 — PYTHONPATH: dev and prod

Fromjacopo <jacopo.pecci@gmail.com>
Date2013-06-11 09:28 -0700
SubjectPYTHONPATH: dev and prod
Message-ID<e76ed600-524e-4e07-81c3-e6592bbd0b11@googlegroups.com>
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

[toc] | [next] | [standalone]


#47682

Fromrusi <rustompmody@gmail.com>
Date2013-06-11 10:14 -0700
Message-ID<2eed235a-5ac2-49d2-933b-1b6d324866ae@n5g2000pbg.googlegroups.com>
In reply to#47673
On Jun 11, 9:28 pm, jacopo <jacopo.pe...@gmail.com> wrote:
> 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>

[toc] | [prev] | [next] | [standalone]


#47780

Fromjacopo <jacopo.pecci@gmail.com>
Date2013-06-12 04:09 -0700
Message-ID<6e5f4c64-59aa-48c3-8759-97af4ff42e1b@googlegroups.com>
In reply to#47682
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.


On Tuesday, June 11, 2013 6:14:43 PM UTC+1, rusi wrote:
> On Jun 11, 9:28 pm, jacopo <jacopo.pe...@gmail.com> wrote:
> 
> > 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>

[toc] | [prev] | [next] | [standalone]


#47781

Fromjacopo <jacopo.pecci@gmail.com>
Date2013-06-12 04:10 -0700
Message-ID<554bb17b-2f38-48e9-8dc7-2ec87bfa0582@googlegroups.com>
In reply to#47682
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. 

On Tuesday, June 11, 2013 6:14:43 PM UTC+1, rusi wrote:
> On Jun 11, 9:28 pm, jacopo <jacopo.pe...@gmail.com> wrote:
> 
> > 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>

[toc] | [prev] | [next] | [standalone]


#47787

Fromrusi <rustompmody@gmail.com>
Date2013-06-12 04:50 -0700
Message-ID<3ba71e9e-fa2a-4ae9-ba22-9a7261b80ff9@be10g2000pbd.googlegroups.com>
In reply to#47781
On Jun 12, 4:10 pm, jacopo <jacopo.pe...@gmail.com> wrote:
> 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.

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]

[toc] | [prev] | [next] | [standalone]


#47801

Fromjacopo <jacopo.pecci@gmail.com>
Date2013-06-12 06:29 -0700
Message-ID<d87cda7a-c98d-4b87-9f65-36c7555f5a26@googlegroups.com>
In reply to#47787
> 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/11536764/attempted-relative-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.

[toc] | [prev] | [next] | [standalone]


#47811

Fromrusi <rustompmody@gmail.com>
Date2013-06-12 09:10 -0700
Message-ID<652e420e-4991-4c46-88d1-c6038bb70f58@lr16g2000pbb.googlegroups.com>
In reply to#47801
On Jun 12, 6:29 pm, jacopo <jacopo.pe...@gmail.com> wrote:
> > 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/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
>
> > 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.

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

[toc] | [prev] | [next] | [standalone]


#47934

Fromjacopo <jacopo.pecci@gmail.com>
Date2013-06-13 03:52 -0700
Message-ID<c3b84960-748b-49aa-9878-359e40eff064@googlegroups.com>
In reply to#47811
On Wednesday, June 12, 2013 5:10:05 PM UTC+1, rusi wrote:
> On Jun 12, 6:29 pm, jacopo <jacopo.pe...@gmail.com> wrote:
> 
> > > 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/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
> 
> >
> 
> > > 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.
> 
> 
> 
> 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

thank you Rusi for your help

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web