0

I'm trying to import the file File.py file from a specific directory, C:\MyPythonFiles. This is what I tried(among several other different attempts):

import sys
sys.path.append("c:\MyPythonFiles")
import File.py

Can someone please tell me how to import File.py from the diretory C:\MyPythonFiles ? enter image description here

Removing the .py so that its 'import File' didn't work either: enter image description here

[UPDATE]

So in my File.py, I had an import for a file that I did not include in c:\MyPythonFiles, so that was a huge problem.

I edited File.py so it only has one function, printHello(). The script works, but PyCharm still does not recognize 'File' for import. I can still use the line File.printHello(), but it's annoying since PyCharm is not autofilling the functions for 'import File'. Any ideas on how to solve this new problem?

enter image description here

[UPDATE] I think @alex answered the original question, and my PyCharm issue should be a seperate question. Thanks for the help @alex!

Robin Alvarenga
  • 321
  • 2
  • 14

1 Answers1

1
import sys
sys.path.append("c:\\MyPythonFiles")
import File

or

import sys
sys.path.append(r"c:\MyPythonFiles")
import File

or

import sys
sys.path.append("c:/MyPythonFiles")
import File

1.Don't need ".py"

2.path link error

Windows path in Python

alex
  • 26
  • 3