Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provide temporary PYTHONPATH on the commandline?

I'm thinking of something like

python3 my_script.py --pythonpath /path/to/some/necessary/modules

Is there something like this? I know (I think) that Pycharm temporarily modifies PYTHONPATH when you use it to execute scripts; how does Pycharm do it?

Reasons I want to do this (you don't really need to read the following)

The reason I want to do this is that I have some code that usually needs to run on my own machine (which is fine because I use Pycharm to run it) but sometimes needs to run on a remote server (on the commandline), and it doesn't work because the remote server doesn't have the PYTHONPATHs that Pycharm automatically temporarily adds. I don't want to export PYTHONPATH=[...] because it's a big hassle to change it often (and suppose it really does need to change often).

like image 622
Ray Avatar asked Aug 18 '16 15:08

Ray


People also ask

How do I change Python path in Terminal?

Use the command line method to add or edit Python Path To set the PYTHONPATH permanently, add the line to your autoexec. bat . Note that before using this method, run echo %PYTHONPATH% . If this gives you a path, proceed with this method; otherwise, set the path as set PYTHONPATH=.;C:\My_python_lib .

What is Pythonpath in Python?

PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find its standard library.

What is the use of pythonpath in Python?

PYTHONPATH Environment Variable in Python. Python’s behavior is greatly influenced by its environment variables. One of those variables is PYTHONPATH. It is used to set the path for the user-defined modules so that it can be directly imported into a Python program. It is also responsible for handling the default search path for Python Modules.

How do I set the pythonpath environment variable for a command?

sys.path.append (r'D:\PyCharmProjects\cheese_shoppe') example in Windows. If you are running the command from a POSIX-compliant shell, like bash, you can set the environment variable like this: If it's all on one line, the PYTHONPATH environment value applies only to that one command.

How to set pythonpath on a Windows machine?

So to set PYTHONPATH on a windows machine follow the below steps: Step 1: Open your This PC (or My Computer) and write click and click on properties. Step 2: After the properties window pop up click on to Advance System Settings:

How to set the pythonpath on Linux to point Python to another directory?

To set the PYTHONPATH on linux to point Python to look in other directories for module and package imports, export the PYTHONPATH variable as follows: $ export PYTHONPATH=${PYTHONPATH}:${HOME}/foo. In this case we are adding the foo directory to the PYTHONPATH. Note that we are appending it and not replacing the PYTHONPATH's original value.


1 Answers

You can specify the python path in an environment variable like so:

PYTHONPATH=/path/to/some/necessary/modules python3 my_script.py
like image 119
wich Avatar answered Sep 29 '22 12:09

wich