How to give path for file passed through command line
Question: I got answer for how to pass file as argument here [How to access variables
from file passed through command
line](http://stackoverflow.com/questions/34596823/how-to-access-variables-
from-file-passed-through-command-line)

But I am asking separately since don't wanna mix two. I am able to access
variables in passed file as `__import__(sys.argv[1])` and called `python
test.py config`. But can I call config.py file by giving pythonpath? e/g/
`python test.py ~/Desktop/config` or `PYTHONPATH='~/Desktop/' python test.py
config`? Because if I do this I get `no module error`.


Answer: You're trying to import a python module using the `__import__` call. It only
accepts the module name. If you need to add a directory to the `PYTHONPATH`,
you can add it to `sys.path` and then import the module:

    
    
    #File: ~/Projects/py/main.py
    
    import sys
    
    python_path = sys.argv[1]
    module_name = sys.argv[2]
    
    sys.path.insert(1, python_path)
    
    print "Importing {} from {}".format(module_name, python_path)
    __import__(module_name)
    

Now I created another file named `masnun.py` on `~/Desktop`:

    
    
    # File: ~/Desktop/masnun.py
    
    print "Thanks for importing masnun.py"
    

Now I try to run `main.py` like this:

    
    
    python main.py ~/Desktop masnun
    

So I am passing the Python path as `argv[1]` and the module name as `argv[2]`.
Now it works. I get this output:

[![enter image description
here](http://i.stack.imgur.com/1AZ0o.png)](http://i.stack.imgur.com/1AZ0o.png)

