Importing Python modules from different working directory -
I have a Python script that uses the built-in module, but it also imports many custom modules present in many directors. The main script itself is
For example, I will call
python agent.py
and import a number of agents, in which The following include:
import check
where a file in the same file should be agent.py
agent / agent / Agent / checks.py
When the current working directory agent is / then all is well. However, if I call agent.py on any other directory, then it is clearly unable to import checks.py and errors.
How can I ensure that custom modules can be imported, regardless of where the agent is from. Py is called as an example
dragon / home / blog / article / finder
Actually your example works because check.py is in the form of agent.py, but it is to say that checks.py was in the first directory, like;
agent / agent.py checks.py
then you can do the following:
path = os There is no path in .path.abspath (os.path.sys.path (join sys.path.insert (1, path) del path
(os.path.dirname (__ file__), '..')) Note Use the __ File __
.
Comments
Post a Comment