Jump to content
Sign in to follow this  
Man In Tan

Import? Import from where?

Recommended Posts

I'm taking a couple of online classes in a couple weeks, but one of them requires fluency in Python. I'm reading some online guides, but the "import" keyword is confusing me. Import from where? In PHP, I would use require to acquire access to my functions, classes, and constants.

require '/my/full/path/to/the/file.php';

But in Python, you use this:

import file

How do I specify the location of the file? Any help would be greatly appreciated!

Share this post

Link to post
Share on other sites

By Default, Python searches in the project directory and hence the files from other directories cannot be imported as we do for the files available locally in the project folder.

But to accomplish that we might want to amend the system path which can be done in the following way.

import syssys.path.insert(1, '/path/to/your/filefolder/')#also sys.path.append(0,/'path/to/your/filefolder/[file])import [your file]


Hope this can help :)

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...