473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Importing from upper directory

I have a script that I want to import called upper.py. It is 2
directories above the script that will call it - i'll call that one
main.py. How am I able to import a script in a directory that is above
it?

Thanks,

Harlin

Feb 17 '07 #1
1 3572
Harlin Seritt wrote:
I have a script that I want to import called upper.py. It is 2
directories above the script that will call it - i'll call that one
main.py. How am I able to import a script in a directory that is above
it?

Thanks,

Harlin
You can control the directories from which Python imports by modifying
the sys.path attribute. Something like this:

import sys
sys.path.append("../..") #That's two directories up from the current
directory
import upper

If you're unsure what the current working directory is, try:

import os
print os.getcwd() # The current working directory

If it's not the current directory, but rather the directory containing
the original script you want, then

import sys, os
path = sys.argv[0] #Initial script name
path0 = os.path.split(path)[0] #Initial script path
path1 = os.path.split(path0)[0] # Up one
path2 = os.path.split(path1)[0] # Up two
sys.append(path2)

Gary Herron

Feb 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vsevolod (Simon) Ilyushchenko | last post by:
Hi, Last year I have written a Perl module to serve as a backend to Macromedia Flash applications: http://www.simonf.com/amfperl I have just rewritten it in Python, which I have not used...
1
by: Henning Kage | last post by:
I have a library with the following structure: library /app /db /gui /interface.py /wx /class.py
2
by: rob | last post by:
Hi all, I've got a csv file which I will need to import into an access db via asp. The csv file will be imported every day of each month so there will be duplicates which will not need to be...
0
by: Julia | last post by:
Hi, I need help with Post build event to copy all projects output dll of a solution to upper directory I didniy succeeded to move to upper folder Thanks.
29
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
5
by: Matt_D | last post by:
Good afternoon. As a self-tutoring project I am writing a one-time-pad encrypt/decrypt script. I have completed the encryption portion and am working currently on the decryption algorithm. My...
0
by: Martin P. Hellwig | last post by:
Hello all, I had some troubles in the past how to arrange my packages and modules, because I usually don't develop my stuff in the Lib\site-packages directory I have some troubles when importing...
3
by: Chanman | last post by:
This is probably a simple question to most of you, but here goes. I've downloaded the xlrd (version 0.6.1) module and placed in in the site-packages folder. Now, when I write a script, I type: ...
1
by: mypetslug | last post by:
Hi, I'm sorry if this has been asked before, but I can't seem to find an answer to this anywhere and so far, trial and error hasn't gotten me far either. Using python 2.4, I've created a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.