How to execute multiple commands in powershell using python? - python

I have the following code:
import os
os.system(cd new_folder)
Now, this code changes the directory to the "new_folder" folder, but what I want to do is make the directory stay in that state so I can conduct another line of code, such as the following:
import os
os.system(cd new_folder)
os.system(md good_folder)
What I intend to do in the above is after I move inside the "new_folder" folder directory, I make another folder named "good_folder" inside the "new_folder" folder.
But I can't seem to find a way to make the directory stay in the same place... how can this be done?

You can have multiple commands separated by semicolons. In your case it would look like:
import os
os.system("cd someFolder; mkdir newFolder")
Refer to this thread for further explanation.

Related

Python can not find file which is searched by imported module

To be specific: My script uses another self-written script, call it sub_script which is placed in an subfolder. Furthermore, in the subfolder is an file which is uses a datasheet, placed in the subfolder, too.
#main_script
from subfolder import sub_script
sub_script.function()
#sub_scribt
import pandas as pd
def import():
data=pd.ExcelFile(Filename.xlsx)
print(data)
Error
[Errno 2] No such file or directory
So I guess this happens is because Python searches in the main-folder section and not in the sub-folder section. But how can I tell Python to exactly do that?
(It would be great if Python does this automatically, that means that it searches the data always in the sub-folder section, independently from the name of the sub-folder).
Thanks for your help!
Python only searches the current directory i.e. the directory that the entry-point script is running from, and sys.path.
try this:
#main_script
import sys
sys.path.insert(1, "/path/to/subfolder") # 0 is the script itself
import sub_script

How to import external scripts in a Airflow DAG with Python?

I have the following structure:
And I try to import the script inside some files of the inbound_layer like so:
import calc
However I get the following error message on Airflow web:
Any idea?
For airflow DAG, when you import your own module, you need make sure 2 things:
where is the module? You need to find where is the root path in you airflow folder. For example, in my dev box, the folders are:
~/projects/data/airflow/teams/team_name/projects/default/dags/dag_names/dag_files.py
The root is airflow, so if I put my modules my_module in
~/projects/data/airflow/teams/team_name/common
Then I need to use
from teams.team_name.common import my_module
In your case, if the root is the upper folder of bi, and you put the scripts of calc in bi/inbound_layer/test.py then you can use:
from bi.inbound_layer.test import calc
And you must make sure you have \__init\__.py files in the directory structure for the imports to function properly. You should have an empty file \__init\__.py in each folder in the path. It indicates this directory is part of airflow packages. In your case, you can use touch \__init\__.py (cli) under bi and _inbound_layer_ folders to create the empty __init\__.py.
Airflow adds dags/, plugins/, and config/ directories in the Airflow home to PYTHONPATH by default so you can for example create folder commons under dags folder, create file there (scriptFileName ). Assuming that script has some class (GetJobDoneClass) you want to import in your DAG you can do it like this:
from common.scriptFileName import GetJobDoneClass
I needed insert the following script inside at the top of ren.py :
import sys, os
from airflow.models import Variable
DAGBAGS_DIR = Variable.get('DAGBAGS_DIR')
sys.path.append(DAGBAGS_DIR + '/bi/inbound_layer/')
This way I make available the current folder packages.

Appending network pc to sys.path

Is it possible to add network pc to sys.path? Currently I'm only able to append a shared folder. When I try to import a script inside this folder I get ImportError. I have the init file in there. As I know this might be because sys.path doesn't actually see the appended folder itself but the files, folders inside?
Because import works from a folder inside the first folder.
directory structure:
//PC023/SharedFolder/folder
so i'm soing this:
sys.path.append(os.path.normpath("//PC023/SharedFolder"))
then I try to import:
from SharedFolder import script -> I get ImportError saying no module exists with this name.
if I do from folder import script2 -> it works
My question is what is the real reason the import doesn't work and can I append the directory like this:
sys.path.append(os.path.normpath("//PC023")) - > because it doesn't seem to work this way

Importing the entire python code that I have written previously

I wrote a python code and saved it
(let's say I saved it in (C:\Users\MyCode\Code1.py) )
and I'm writing a new python code, and I would like to copy everything from Code1.
I just opened Code1.py and copied and pasted it, but is there a smarter way to do it?
(thanks for the answers so far, but let's assume that the code that I'm trying to copy and the code that I'm writing are not in the same directory)
You can do something like this:
Code1.py
def hello():
print("Hello")
New file
import Code1
Code1.hello() # "Hello"
This way you don't have to copy and paste all the code.
If the file you are trying to import is not in the same directory...
Here is a possible answer to your question taken from here.
By default, you can't. When importing a file, Python only searches the current directory, the directory that the entry-point script is running from, and sys.path which includes locations such as the package installation directory (it's actually a little more complex than this, but this covers most cases).
However, you can add to the Python path at runtime:
# some_file.py
import sys
sys.path.insert(0, '/path/to/application/app/folder')
import file
If both files are on the same directory you could try:
from Code1 import *
You could also include your file to the python path. That is, if you have something like this:
dir\
Code1.py
other_dir\
code.py
You could do in code.py:
import sys
sys.path.append('..')
from Code1 import *
This works (by inserting its parent directory to the path) but I would avoid doing this on production code as seems very wrong. Instead you may want to check out python packaging (https://packaging.python.org/).

Add Python script so can be imported in another Python script

So I know in windows you can just add the file to the Lib folder and then just add import filename to the python script.
Is it possible to do this on Ubuntu in anyway as I need to import this file to make a project work. Link to file need to access
I toured the github repo for a bit,
it should be possible to simply copy CMUTweetTagger.py to your folder where yourapp.py is located (same level) then
import CMUTweetTagger
CMUTweetTagger.runtagger_parse(...)
Alternatively, since ark-tweet-nlp-python is a package (has got __init__.py in it)
You can copy the whole ark-tweet-nlp-python folder into e.g. ark_tweet_nlp_python folder (again same level as your script), e.g. by cloning it
Git clone:
git clone https://github.com/ianozsvald/ark-tweet-nlp-python ark_tweet_nlp_python
Use it as a module:
from ark_tweet_nlp_python import CMUTweetTagger
You need to:
import sys
print sys.path
sys.path.append('/path/to/lib/dir')
import mylib
Also, if the python script you are trying to import is in a directory, you need to make sure that you have an init.py file in that folder. It can be empty (touch /path/to/lib/dir/__init__.py) before an import will work. You can ls /path/to/lib/dir to see if there is an init.py file.

Categories

Resources