0
Aim:-
I would want to set python path programmatic in my project. So that, all other
directories files can be imported without any issues.
Problem statement:-
I used to add all the folders & sub-folders in environment variables under
PYTHONPATH but this has two constrains.
1, Any new folders created need to be added in environment variables all the
times. After certain limit, environment variables will not be accepted new path
reference due to characters limitaion.
- Error: This environment variable is too large. This dialog allows setting
values up to 2047 characters long.
2, Secondly, all the team members in my team need to perform the same activity
manually all the times.
Tried self solution:-
Created a sample folder and added the below code before main import files and
ran from command prompt which worked perfectly fine.
**FilePath: foo/Python/Learning&Development/Pratice/Directory1/File1.py**
class File1():
def parent(self):
return "I am from File1"
**FilePath: foo/Python/Learning&Development/Pratice/Directory2/File2.py**
import sys
try:
sys.path.index('foo/Python/Learning&Development')
sys.path.index('foo/Python/Learning&Development/Pratice')
sys.path.index('foo/Python/Learning&Development/Pratice/Directory1')
sys.path.index('foo/Python/Learning&Development/Pratice/Directory2')
except ValueError:
sys.path.append('foo/Python/Learning&Development')
sys.path.append('foo/Python/Learning&Development/Pratice')
sys.path.append('foo/Python/Learning&Development/Pratice/Directory1')
sys.path.append('foo/Python/Learning&Development/Pratice/Directory2')
from Pratice.Directory1.File1 import File1 as f
class File2():
def child(self):
return f.parent(self)
Results:
I am from File1
Now I wish to convert the sys.path as a single method and call automatically
before running any .py file in the project folder.
So, on trail basis, I created 'init.py' file added the same piece (sys.path) of
code, commented the same code in File2.py and run it but ended up with file
import error.
**FilePath: foo/Python/Learning&Development/Pratice/Directory2/__init__.py**
import sys
try:
sys.path.index('foo/Automation/Python/Learning&Development')
sys.path.index('foo/Automation/Python/Learning&Development/Pratice')
sys.path.index('foo/Automation/Python/Learning&Development/Pratice/Directory1')
sys.path.index('foo/Automation/Python/Learning&Development/Pratice/Directory2')
except ValueError:
sys.path.append('foo/Automation/Python/Learning&Development')
sys.path.append('foo/Automation/Python/Learning&Development/Pratice')
sys.path.append('foo/Automation/Python/Learning&Development/Pratice/Directory1')
sys.path.append('foo/Automation/Python/Learning&Development/Pratice/Directory2')
**FilePath: foo/Python/Learning&Development/Pratice/Directory1/File1.py**
class File1():
def parent(self):
return "I am from File1"
**FilePath: foo/Python/Learning&Development/Pratice/Directory2/File2.py**
# import sys
# try:
# sys.path.index('foo/Python/Learning&Development')
# sys.path.index('foo/Python/Learning&Development/Pratice')
# sys.path.index('foo/Python/Learning&Development/Pratice/Directory1')
# sys.path.index('foo/Python/Learning&Development/Pratice/Directory2')
# except ValueError:
# sys.path.append('foo/Python/Learning&Development')
# sys.path.append('foo/Python/Learning&Development/Pratice')
# sys.path.append('foo/Python/Learning&Development/Pratice/Directory1')
# sys.path.append('foo/Python/Learning&Development/Pratice/Directory2')
from Pratice.Directory1.File1 import File1 as f
class File2():
def child(self):
return f.parent(self)
Results:
Traceback (most recent call last):
File "File2.py", line 13, in <module>
from Pratice.Directory1.File1 import File1 as f
ModuleNotFoundError: No module named 'Pratice
Could someone help me on this problem? I would want the same piece of code to
be executed before running any of the .py file in my project folder. So that,
import error would not come into picture.
Expected: Python path should be set automatically by default and import error
should not occur while running any python file in the project.
--
https://mail.python.org/mailman/listinfo/python-list