Python: coding to account for multiple development environments?

97 views Asked by At

I am switching back and forth between writing Python scripts using Visual Studio 2015 and PythonAnywhere.

While I can copy my .py files between the two development environments, there are some changes that have to be made to run the same script. For example, my path to Chromedriver in VS might be C:/python27/libs/site-packages/... , but when I flip to PythonAnywhere, I want to use Firefox, and its driver is in ./drivers/... The imports between the two may be slightly different.

I don't even know if this is possible, but is there some way to put something at the beginning of the script that tells the interpreter:

if running this script on PythonAnywhere: 
   make these assumptions
if running this script on VisualStudio:
   make these assumptions

...and then I don't have to keep tweaking back and forth?

I apologize if I don't have the right nomenclature for describing what I'm trying to do. I see that there's something called pyenv, but it seems to be an aid for Python 2.x vs 3.x projects (which isn't my boggle here.)

1

There are 1 answers

0
hwjp On BEST ANSWER

PythonAnywhere dev here. There are probably a few different ways you could do this. Here's the first one that occurs to me, which is based on the fact that your PC has Windows whereas PythonAnywhere is on linux

# at the top of your scripts:
import sys
ON_PYTHONANYWHERE = sys.platform == "linux"

#... later in your code
if ON_PYTHONANYWHERE:
    browser = webdriver.Firefox()
else:
    browser = webdriver.Chrome()