Reputation: 13
I have a series of .py files that I am using a .bat for to automate the whole process. Within some of the .py files I have called variables from another .py file. I did this to allow myself to only change one .py when running new processes. I have the .bat set up to run normally with the .py files, but I do not understand how to incorporate the variables.py file.
Example:
Python1.py
import os
import somefolder.variables as variables
path = 'C:/pictures/'
file = variables.file
for img in os.listdir(path + variables.file):
print(img)
Variables.py
file = item1.img
Batch.bat
call "C:/Users/name/anaconda3/Scripts/activate.bat" "C:/Users/name/anaconda3/envs/environment"
call "C:/Users/name/anaconda3/envs/environment/python.exe" "D:/Scripts/python1.py"
...
After running something similar to this. I received the error: No module named 'somefolder'
. I've read a few other posts about using the echo
command or using set
, but it seems as that is setting a variable within the batch and not calling it from another .py file. I am farily new to batch files, so any help would be appreciated.
Upvotes: 0
Views: 66
Reputation: 13
As @Rashid 'Lee' Ibrahim mentioned in the comments above, it would be best to look into using __intit__.py
. Though for the sake of getting the code to run, I set where the module/.py file was located as a system path.
Python1.py Original
import os
import somefolder.variables as variables
path = 'C:/pictures/'
file = variables.file
for img in os.listdir(path + variables.file):
print(img)
Python1.py Edited
import os
sys.path.append('D:/Scripts/')
import variables
path = 'C:/pictures/'
file = variables.file
for img in os.listdir(path + variables.file):
print(img)
Upvotes: 1
Reputation: 11
Read file contents into a variable:
for /f "delims=" %%x in (version.txt) do set Build=%%x
or
set /p Build=<version.txt
Both will act the same with only a single line in the file, for more lines the for variant will put the last line into the variable, while set /p will use the first.
Using the variable – just like any other environment variable – it is one, after all:
%Build%
So to check for existence:
if exist \\fileserver\myapp\releasedocs\%Build%.doc ...
Although it may well be that no UNC paths are allowed there. Can't test this right now but keep this in mind.
Upvotes: 0