This is just an idea, which may not in practice be a major problem but can at 
times be an inconvenience.  I thought I had posted this in the "python -m" 
thread a little while back but checking my history it does't appear I did, I do 
apologize if this is a duplicate.
When running a python script directly, the directory of that script gets added 
to sys.path.  When running as a module "python -m", it looks like an empty 
string gets added to sys.path, which appears to result in CWD being used.
But, depending on the directory structure, modules imported may not be the 
expected modules.  For instance, if developing a package 'mypackage', with some 
OS specific code organized into an os module or subpackage "mypackage.os", and 
then running something like "python -m pytest" or "pylint ..." in the directory 
for the package would cause an "import os" to treat the mypackage/os(.py) 
module or package as the top level import and cause errors.  (I've actually had 
this happen which is what prompts this idea)
There are other solutions that work right now:
Don't develop a subpackage or submodule with the same name as a python package 
or module, or any other package or module that may be on sys.path.  I think 
this is unrealistic as a future addition could result in a package being added 
that wasn't previously present and there is no way to know the names of all 
third-party packages
Just change the directory and run the script/module from another directory that 
would not cause an issue.  This works great, but seems to go against not 
presuming where the script is run from or what the CWD is.
Install a script in ~/bin or somewhere to do the same at least for modules.  
This would set the CWD to ~/bin, and not likely to have any conflicting 
packages or modules, but still presumes that it must not have a conflicting 
module.  What if my ~/bin happens to have other ".py" files marked as 
executable that is used for something else. It then becomes a top-level module 
able to be imported, and possibly interfere with other imports.
I'd like to propose a python option which would disable the addition of this 
directory directory.  Essentially something to make it possible to say "import 
the installed module xyz and not perhaps some script or package sitting right 
beside me also named xyz".  Something like "python3 --no-add-cwd-to-path -m 
pytest".  This would allow in cases where needed, to prevent added the script 
path or CWD to the sys.path list.
This could already be done directly in code:
import sys ; sys.path.pop(0)
But having an option seem to be more flexible as it give the user of the script 
the ability to say "don't add this current directory to the python path" for 
existing scripts and modules without having to edit them, create a custom 
launcher script, or change directories, etc.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/372DEVQ2MBUY76KV5ZK6HK3UONBYROTU/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to