Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import path
#1
Hi everyone,

how can I simply do an import of a file who is in another directory ?

I tried

import ..\myfile
import ..\myfile.py
from ..\folder import myfile

But nothing work Cry
[Image: NfRQr9R.jpg]
Reply
#2
Try to be more specific and clear in your query, it will help get you the answer and help you need.

What is it you are trying to do? Bring in a file from another folder?
What format is the file you are trying to get?
How is the file to be used or displayed?

Where is your full code and the errors being given when you run it?
A lot can be determined by seeing what you have and the errors being flagged when it is run.
"Often stumped... But never defeated."
Reply
#3
Sounds like he has a module that reverences another module in a different directory. I think Python is severely lacking in how it manages modules, but most likely this reveals my ignorance more than anything else. Importing packages with pip is fantastic, but when trying to do anything much larger than a script or simple application I do not see the clear path on how to set up directories and paths and __init__.py files and when to use relative imports and when they don't work and when you need to use -m when your run a module. Maddening!

It is easy to import modules that are on your python path, but not so easy to add a directory to the python path. It is mystifying that you can get yourself in a situation where you can import a module from one directory, but when you try to import from a different directory you have to do it in a different way, and that you have to run the program in a different way. The most maddening part is how this is all glossed over in everything I can find about packages and modules and directories.
Reply
#4
It can be a little confusing to a make package,i have an answered in many post about this.
Can post some of these link so you can a take look.
link-1, link-2 and tutorial that has more about wheel, pip, setup.py, package and disturbed on PyPi.
Reply
#5
Thank you all, for your inputs !

I will clarify what I'm trying to do.


I have a simple script.py (for learning purpose) located for example in:
C:\users\SpongeB0B\Python\test99\script.py

I just discover the function import who allow to import another .py file within the current code. Like for Autoit the #import see here

Let say I have a .py one directory up aka cd .. C:\users\SpongeB0B\Python\functions.py

How can I import it ?

I tried
import ..\functions.py
import "..\functions.py"
import ..\functions
import "..\functions"
but none of those work.. can we even go one level up ??
[Image: NfRQr9R.jpg]
Reply
#6
I've found a work around

sys.path.append('the path where is located the .py file that I want to include..')
not totally what I was looking for. But this will do the trick.

I've found a great article about import btw
https://www.pythonforthelab.com/blog/com...-and-more/
[Image: NfRQr9R.jpg]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Import a module one step back of the path prathampatel9 1 1,067 Sep-21-2022, 01:34 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,198 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  'import Path' do not understand how to use it tester_V 2 1,631 Jun-19-2021, 02:23 AM
Last Post: tester_V
  ImportError: cannot import name 'path' user123 2 12,434 Jul-14-2019, 10:08 AM
Last Post: Larz60+
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,733 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020