Using invoke, how can I change the directory that the run call operates in?
In Fabric, one would
from fabric.context_managers import lcd
with lcd('foo'):
local('do')
to run do in the foo directory, but I can't find a similar import in pyinvoke.
as simple as that
import os
os.chdir(path)
Use Context.cd
Take a look at docs
with ctx.cd('/path/targeted'):
# do something in /path/targeted
Related
I tried writing some commenly used function in a seperate file and import the same into mainApp file, but not able to use import.
I did find many questions regarding the this same question but, the solution was to keep the files in the same folder
I tried without .py as well, but the same error:
Can you please help me how can i fix this issue ?
No '.py'. Just import seperate
Try using this in mainApp.py:
from seperate import *
a()
where seperate.py looks like this:
def a():
print('hi')
Well, sorry, those two files need to be in the same folder. This is not a solution to your problem.
The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported. Here are a few examples of relative imports:
from .some_module import some_class
from ..some_package import some_function
from . import some_class
Read more about Absolute vs Relative Imports in Python
In your case it should be:
from .seperate import a
Also check this question:
Importing from a relative path in Python
add your project directory into your path variable so that python know from where you want to import file
I have 10 different python projects stored in one folder (F:\Python_Code...). I want to call user define functions from 10 different projects into the last project (Say Project11) and by running Project11, all my 10 projects should run one by one.
I have tried multiple ways like os.path() and from project1 import function, etc. but no one work. I read about the change in PYTHONPATH, but I am still not able to do that. I am using PyCharm. Can anyone help me to solve the problem?
soni smit!
Your solution wasn't that far away.
First you have to import the whole file with:
from . import filename
or just
import filename
if the file is in the same directory as your main file.
then you can call a function from that file with:
filename.functionname(arg1, arg2, ...)
I hope, it works for you!
~ostue
It's not a good idea to reference an upper-level directory for importing your packages.
If you're sure of what you're doing, you can change the working directory using os.chdir(path_to_dir_that_can_access_all_your_modules).
If you need the flexibility to import your libs in a dynamic way, try using importlib.import_module('module_name').
ex.:
import os, importlib
def import_module(base_path, module_path):
try:
backup_wd = os.getcwd() # backup original working directory
os.chdir(base_path) # change directory
return importlib.import_module(module_path) # import and return your module
except:
# Handle problems
...
finally:
os.chdir(backup) # go back to original directory in any case
project10_module = import_module('F:\Python_Code', 'project10.utils.yourmodule')
module_instance = project10_module(args)
So in the past when I've used a unix server to do my python development if I wanted to pass in an entire folder or directory, I would just put an asterisk() on the end of it. An example would be something like users/wilkenia/shakespeare/ to pass in a set of files containing each of shakespeare's plays. Is there a way to do this in windows? I've tried putting in C:\Users\Alexander\Desktop\coding-data-exam\wx_data* and the same with the disk name removed. Nothing has worked so far, in fact, it takes in the directory as an argument itself.
Edit: implemented glob, getting a permissions error, even though I'm running as administrator. Here's my code if anyone wants to have a look.
For the sake of showing how you can use pathlib to achieve this result. You can do something like this:
some_script.py:
from pathlib import Path
path = Path(sys.argv[1])
glob_path = path.glob('*')
for file_path in glob_path:
print(file_path)
Demo:
python some_script.py C:/some/path/
Output:
C:/some/path/depth_1.txt
C:/some/path/dude.docx
C:/some/path/dude.py
C:/some/path/dude_bock.txt
The nice thing about pathlib, is that it takes an object oriented approach to help work with the filesystem easier.
Note: pathlib is available out-of-the-box from Python 3.4 and above. If you are using an older version of Python, you will need to use the backported package that you can get from pypi: here
Simply: pip install pathlib2
You can use the glob module, it does exactly this.
A quick demo:
In [81]: import glob
In [82]: glob.glob('*')
Out[82]:
[
... # a bunch of my personal files from my cwd
]
If you want to extend this for your use case, you'll need to do something along the lines of:
import sys
import glob
arg = sys.argv[1]
for file in glob.glob(arg):
....
You'll read your args with sys.argv and pass it onto glob.
I am using Python 2.7 on Windows 7 Professional.
I am trying to call a function saved in another file to run in this file's code.
Function called dosomething is found in anotherfile.py
anotherfile.py is in the same directory as current code.
My call in this file is simple:
import anotherfile
print anotherfile.dosomething
I am getting an error: No module named anotherfile
The problem is the same as I found in this post
I don't understand the solution but I'd like any insight?
Thank you.
EDIT: The other question/answers discuss resetting CLASSPATH and setting PYTHONPATH. I explored this but was not sure how to do this. Perhaps relevant?
Let us have two files in the same directory. Files are called main.py and another.py.
First write a method in another.py:
def do_something():
return "This is the do something method"
Then call the method from main.py. Here is the main.py:
import another
print another.do_something()
Run main.py and you will get output like this:
This is the do something method
N.B.: The above code is being executed using Python 2.7 in Windows 10.
Specify the module then the file then the import like so:
from this_module.anotherfile import dosomething
or if you want all functions from "anotherfile.py"
from this_module.anotherfile import *
and then you can call the "dosomething" command without the "anotherfile" prefix.
I ran into same problem. After ample of trials, I ended up solving it with the below mentioned solution:
Make sure your current file and anotherfile.py lies in same location of system path.
Say your another.py and current file lies at location : "C:/Users/ABC"
In case, one is not aware of system path. Use below code in current file:
import sys
print(sys.path)
import sys
sys.path.append('/C:/Users/ABC/')
Then you do below code in same current code:
from another import dosomething
I found the issue. Python was looking in another directory for the files. I explicitly set my working directory as the path to where thisfile.py and anotherfile.py reside and it works. Thank you for all the quick replies.
Want to move up one directory from a given directory. I achieve this by doing:
import os
os.chdir(given_dir)
os.chdir('..')
But, I was wondering if there was a better more explicit way using (ideally) one statement or if there exists a built-in feature that I may not be aware of.
How about
import os, os.path
print os.chdir(os.path.join(given_dir, os.pardir))
OR
os.chdir(os.path.dirname(given_dir))
(as Selcuk suggested)