0

Newbie here

I want to import a class from a file in another directory, in this case: C:\Users\jose_\Desktop\my_ref\stack_class.py, using the from - import statement. I have an init.py file in said directory but cannot find the right sintax anywhere.

How do you put C:\Users\jose_\Desktop\my_ref\stack_class.py in the import part?

I have tried using these other two ways:

exec(open("file path on windows").read())

and the import sys and sys.path.insert

they both work, trying to do it with the import statement.

from C:\Users\jose_\Desktop\my_ref\stack_class.py import Stack #Error here
foo = Stack()

print(type(foo))

Error

  File "C:/Users/jose_/OneDrive/Mis_Documentos/Educacion/Linkedin/Ex_Files_Python_Data_Structures_Queues/Exercise_Files/Ch02/02_02/End/main2.py", line 4
    from C:\Users\jose_\Desktop\my_ref\stack_class.py import Stack
          ^
SyntaxError: invalid syntax
ComplicatedPhenomenon
  • 4,055
  • 2
  • 18
  • 45
joseag312
  • 3
  • 1
  • 5

3 Answers3

0

Did you make sure to switch your forward slashes "\" with back slashes "/" ?

barker
  • 1,005
  • 18
  • 36
0

try using:

sys.path.extend('file path here') 

to add the folder path to your python path, that way python knows where to look for the stack_class module - either that or navigate to the folder before importing

also make sure in the folder your init file is called __init__.py

you should then be able to do

from stack_class import Stack
Derek Eden
  • 4,403
  • 3
  • 18
  • 31
  • anyway to do it directly without appending to sys.path? only using import. – joseag312 Jul 18 '19 at 02:05
  • bunch of options here https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path, or could navigate to the folder before importing...but all kind of overly lengthy in my opinion...is there a reason why you dont want to add it to your path? definitely the simplest way and you only have to do it once – Derek Eden Jul 18 '19 at 02:14
  • Is it considered best practice to append to path? – joseag312 Jul 18 '19 at 03:22
  • if it's a package you want to permanently have access to then I would say yes, either that or just add it to your site packages folder where the rest of your python packages are stored – Derek Eden Jul 18 '19 at 12:54
0

As far as I know, you cannot import from a file path in Python. You can only import by passing a module name:

import module.submodule
# then
module.submodule.sth()

or

from module.submodule import sth
# then
sth()

And Python will search module/submodule.py and module/submodule/__init_.py in all the directories in sys.path.

So if you want to import from a file in another directory created by yourself (not included in the sys.path list) you have to append that directory to the list to tell Python where to find that module. Try to run:

import sys
sys.path.append('C:\\Users\\jose_\\Desktop\\my_ref')
from stack_class import Stack
# then
foo = Stack()
print(type(foo))

Edit

If you don't want to append to sys.path, you need to move your module to one of the diretories in sys.path, for example, the current directory . is also in sys.path, or Python won't know where the module is (C:\ is not in sys.path). Python never imports from directories that are not included in sys.path (via import statements, I mean)!

Xinhe Wang
  • 145
  • 2
  • 9
  • sys.path.append doesn't permanently add it to the path..you'd have to do this every single time you want to import the package... sys.path.extend is permanent, you only do it once – Derek Eden Jul 18 '19 at 12:59
  • 1
    @DerekEden Both `append` (``) and `extend` (``) are list methods. But `extend` accepts an iterable: `help(list.extend)` `extend(self, iterable, /) Extend list by appending elements from the iterable.`; `append`: `help(list.append)` `append(self, object, /) Append object to the end of the list.`. So I think `sys.path.append('/path/to/module')` and `sys.path.extend(['/path/to/module'])` are the same. – Xinhe Wang Jul 19 '19 at 10:01
  • sorry you are definitely right..I confused myself with another way to permanently add to your path because I had this issue before..easiest way to permanently add a module to your path is to add the folder to your PYTHONPATH environment variable, that way you only have to add it once and will always be able to import it using "from stack_class import Stack" – Derek Eden Jul 19 '19 at 12:30