3

I want to pass PYTHONPATH as argument to python.exe just like i can do this in java:

java -classpath /somedir/some.jar;/anotherdir MyClass

so i'm looking for something like:

python -PYTHONPATH /somedir/pythonsrc;/anotherdir/pythonsrc mymodule.py

is it possible to do such a think in python? thanks

4
  • That's not possible like that but why not use old good set PYTHONPATH=...? Commented Apr 6, 2012 at 13:59
  • that's because i have multiple python apps and i want a simple command line (not "fancy" batch scripts because they have their own disadvantages) i want a simple command line to run each of my apps with different libraries! that seems to me like the first requirement of a program language, i use this so much in java, java -cp mylibraries myclass. i have multiple python apps with different languages! i need to run each with its own command line - no batch script!!! i find it unbelievable there is no such option! can you help? Commented Apr 7, 2012 at 8:25
  • See Setting environment variable for just one command in Windows cmd.exe Commented Apr 7, 2012 at 22:29
  • The question is still relevant. I have a bash script that spawns a subprocess to run Python and feeds its output into another Python process. It would be great to set different PYTHONPATHs explicitly, because my "general" PYTHONPATH includes a relative path (. -- always nice to have, but may interfere with other libs) but they are executed in different directories. However, it's still not possible to do. Commented Jun 29, 2021 at 13:14

2 Answers 2

3

Try to set the environement variable PYTHONPATH before launching python :

On windows :

set PYTHONPATH=/somedir/pythonsrc;/anotherdir/pythonsrc && python.exe mymodule.py 

On unix

PYTHONPATH=/somedir/pythonsrc:/anotherdir/pythonsrc python mymodule.py 
Sign up to request clarification or add additional context in comments.

1 Comment

You can use PYTHONPATH=... python mymodule.py to set the variable just for that invocation.
0

Start a new shell using /C option.

cmd.exe /C "set PYTHONPATH=/somedir/pythonsrc && python.exe mymoudle.py" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.