0

When I try to do using sys in following way, it gives error: name 'path' is not defined. Here test.py is file in folder directory. Also folder directory has __init__.py in it.

import sys
sys.path.append('c/Users/Downloads/folder')
from test import *
print(path)

Content of test.py

path="sample"

I also tried writing from test import path but it did not work. So is there any other way than sys. I am not able to find why it's not working with it, since most answers recommend this.

EDIT: Python is used a lot in industry so I am hopeful there must be some way for that (without using or using sys). I can have multiple files with same name in my system so help provided in answer and comments are not permanent fix.

  • 1
    You forgot the `:` after `c` – Barmar Jun 14 '21 at 17:22
  • Shouldn't it be `print(sys.path)`? – fuzzydrawrings Jun 14 '21 at 17:27
  • @Barmar It still gives same error –  Jun 14 '21 at 17:27
  • @fuzzydrawings that didn't worker either –  Jun 14 '21 at 17:27
  • Do you have `test.py` in another folder in `sys.path`? – Barmar Jun 14 '21 at 17:28
  • @fuzzydrawings He doesn't want to print `sys.path`, he wants to print the `path` variable from `test.py`. It's just a coincidence that they have the same variable name. – Barmar Jun 14 '21 at 17:28
  • @KRS7784 It seems to work if you use `sys.path.insert(0,'C:/Users/Downloads/folder')` (instead of `sys.path.append`) – fuzzydrawrings Jun 14 '21 at 18:11
  • `test` collides with a standard library module name. use a different name. – wim Jun 15 '21 at 03:41
  • @wim but if I have multiple project (or even in same project) with similar file names and used for similar purpose, I need some method other than sys. That was my main question. –  Jun 15 '21 at 08:21
  • @Barmar yes, but how to do without sys (because I need to use similar file names in same system). –  Jun 15 '21 at 08:22
  • @fuzzydrawings yes that works. But suppose I want to have multiple files, then it would be trouble for me. That is why asked how to do without sys ? –  Jun 15 '21 at 08:23
  • `sys` works perfectly as a solution if you use `sys.path.insert(0,'path/to/folder')`. I'm not sure why you don't want to do that? – fuzzydrawrings Jun 15 '21 at 18:33

1 Answers1

1

This is an interesting problem so I spend a little time investigating. After some digging, it turns out the problem is there is already another script named "test.py" in one of the sys.path directories and Python is importing that script instead of the one you want.

When you import a module, Python looks for it in each of the search paths in the order they are listed in sys.path and imports the first match. Using sys.path.append('path/to/folder') fails because it adds a search path to the end of the list, and Python already finds a match for test.py in one of the directories listed before it. This is also the reason sys.path.insert(0,'path/to/folder') works - because it inserts the specified path at the front of the list so Python will find it first.

FIX

  • Change the name of the file 'test.py' to be something unique that won't match any other file names. This is the probably the best way.

  • You could use sys.path.insert(0,'path/to/folder') which places the specified path first in the list of search paths.

  • Another way is to go through each of the directories in the sys.path list and remove or rename the other "test.py" file(s). Not recommended

is there any other way than sys

If both the script you are running and the script you want to import are in the same folder, you don't need sys module.

If the files are in different folders and you absolutely won't use sys, then you can set PYTHONPATH in terminal before running the script. Note that you must run the python script from inside this same shell for it to work. Also, this will not work if the folder your script is in also contains a file with the same name as the one you want to import from another folder

Linux Bash:

export PYTHONPATH="path/to/folder/"

Windows Powershell:

set PYTHONPATH="path/to/folder/"
fuzzydrawrings
  • 283
  • 2
  • 6
  • So in this if I already have some other python files with same name, then I cannot use similar name for same purpose. It worked after I renamed it test1.py. But I always have to think of unique file name in this approach. Is there any way to do this without using sys ? –  Jun 15 '21 at 03:19
  • @KRS7784 Yes, if both files are in the same directory. See edited answer. – fuzzydrawrings Jun 15 '21 at 03:41
  • I knew that same directory thing, but when we do big projects files can be in parent directory (or in general anywhere in the system). Python is used a lot in industry so I am hopeful there must be some way for that. –  Jun 15 '21 at 08:19
  • See edited answer. You can set `PYTHONPATH` before running the script. – fuzzydrawrings Jun 15 '21 at 17:28