[Tutor] Python project organisation

dn PythonList at DancesWithMice.info
Sat Apr 22 22:23:49 EDT 2023


On 23/04/2023 13.30, Phil wrote:
> On 23/4/23 10:49, dn via Tutor wrote:
> Thank you DN.
> 
>> Yes, each project SHOULD be 'insulated' from the next. 
> 
> What I've decided to do is only create a project directory and virtual 
> environment for those projects where it makes sense. The rest can stay 
> in the global environment.

That makes good sense - the effort of re-arranging every project (and 
testing to ensure it still works - correctly) might be quite large! 
What's the point? You might never work on that project again...

If/when you come to work on a project, the first step could be to 
modernise its project-structure according to your new methods - and some 
benefit will result from the effort along-side the planned upgrade!


>> The mode of utilising virtual-environments is that only one (or a few 
>> related) project/environments will be open or "live" at a time. Part 
>> of making an environment active is to add it into the PYTHONPATH. 
>> Conversely, part of shutting-down the environment, eg when you move 
>> your attention to a different project, is to remove those references 
>> again, ie back to system-settings. Thus, the modification to 
>> PYTHONPATH is only temporary.
> 
> I knew about activate but I didn't know that the addition to the 
> PYTHONPATH is temporary. I suppose it's only a matter of activating a 
> new projects environment to deactivate the previous one?

Not exactly.

It is possible (not speaking for VS-Code) to work on two projects 
concurrently, eg some analysis script may need your email library to 
present reports or alerts. Because your email library may be used by 
other projects, it is in a library/project of its own. Thus, you may 
need/prefer to have both 'projects' open, to ease integration.

The "deactivation" will happen when you *close* a project.
(at least that's how PyCharm does it).

Thus, even taking the (perhaps) unusual illustration, above, when each 
project is closed, deactivation happens. After both have been closed, 
the PYTHONPATH will be back to its original system-wide settings - with 
no 'local directory' included.


>> Presumably VS-Codium/e (etc) will offer something equivalent.
> 
> Yes it does and it finds a newly created virtual environment 
> automagically. I think VS Code is able to change the activation of 
> different project environments but I'm a little hazy on the exact 
> procedure. It seems that starting VS Code from within a project's 
> virtual environment is the easiest option.

VS-Code is not used by any of my project teams currently, so am loathe 
to offer a categorical reply. However, that sounds a bit 'backwards'/too 
much manual effort.

In PyCharm, one selects a (new) project to work within. At that time, 
the IDE activates the virtual-environment AND sets-up the 'walls' which 
keep anything from 'leaking' (between projects, or between this project 
and the system). Part of the activation is to make the project's 
(virtual) directory the current working-directory, ie the 
project-settings define the directory, not your locating the directory 
first.

(from memory, there's a subtlety to such, which may not apply in 
SublimeText - but as neither of us is using that IDE, consider it ignored!)

In other words, nominating the project is sufficient for the IDE to 
effect all that's necessary; and vice-versa when the project is closed.

What do the VS-Code user-docs say?
(for comparison: 
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html and
https://www.jetbrains.com/help/pycharm/poetry.html)

-- 
Regards =dn

-- 
Regards,
=dn


More information about the Tutor mailing list