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?
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 PYTHONPATH
s 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).
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 .
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.
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.
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.
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:
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.
You can specify the python path in an environment variable like so:
PYTHONPATH=/path/to/some/necessary/modules python3 my_script.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With