Do I need to install Python separately to use os module

Label printing integration (NiceWatch middleware and ActiveX programming interface)

Moderators: Georges, milos, NiceLabel Support Team

Post Reply
FunDeckHermit
Enthusiast
Posts: 14
Joined: Fri Apr 20, 2018 1:58 pm

Do I need to install Python separately to use os module

Post by FunDeckHermit » Tue Jun 26, 2018 4:46 pm

Hello again,

I'm trying to write a simple logger with Python.
I can't switch back to VBscript so Python it is.

Code: Select all

import os
import datetime

today = datetime.date.today()
Filename = today.strftime('%Y%m%d Nicelabel log.txt')
directory = "Logs"
if not DoesFileExist(directory + "/" + Filename):
	if not os.path.exists(directory):
		ShowMessage("No such directory", "caption")
		os.makedirs(directory)	
	ShowMessage("No such file", "caption")
	CreateFile(directory + "/" + Filename)
WriteLineToFile(directory + "/" + Filename, "Hello World")
It gives me the error: "no module called os"
This is weird because the os module was added in the script editor from the left pane.

Do I need to install Python separately from Nicelabel? What version of Python do I need?
Last edited by FunDeckHermit on Thu Jun 28, 2018 11:27 am, edited 1 time in total.

User avatar
Saso
NiceLabel
NiceLabel
Posts: 2937
Joined: Mon Sep 04, 2006 8:09 am
Contact:

Re: No I need to install Python separately to use os module

Post by Saso » Wed Jun 27, 2018 7:33 am

NiceLabel 2017 comes with pre-installed IronPython. IronPython is .NET variant of Python. It runs on .NET Framework and allows you to use .NET assemblies. Read more about it on IronPython site here. You can use the classes/functions from .NET.

You can also use "classic Python modules", such as "os" or "datetime", but in this case you have to install Python on the computer running NiceLabel. See KB article.
Saso Fleiser
Senior Technical Product Specialist

FunDeckHermit
Enthusiast
Posts: 14
Joined: Fri Apr 20, 2018 1:58 pm

Re: Do I need to install Python separately to use os module

Post by FunDeckHermit » Wed Jun 27, 2018 9:11 am

Thank you Saso,

This was exactly what I was looking for.
I have included my logger for future references:

Creating directory and file:

Code: Select all

from System.IO import Directory
from System.IO import File
import datetime

today = datetime.date.today()
myFilename = today.strftime('%Y%m%d Nicelabel log.txt')
myDirectory = "Logs"
myFullRelativePath = myDirectory + "/" + myFilename

if not File.Exists(myFullRelativePath):
	if not Directory.Exists(myDirectory):
		ShowMessage("No such directory", "caption")
		Directory.CreateDirectory(myDirectory)	
	ShowMessage("No such file", "caption")
	File.Create(myFullRelativePath).Close()
Appending to file:

Code: Select all

import datetime
from System.IO import File

today = datetime.date.today()
myFilename = today.strftime('%Y%m%d Nicelabel log.txt')
myDirectory = "Logs"
myFullRelativePath = myDirectory + "/" + myFilename

with File.AppendText(myFullRelativePath) as sw:
	sw.WriteLine("Hello")
	sw.WriteLine("World")

Post Reply