0

This is what I'm trying to do.

But after that problem resolved, I had this error: ImportError: No module named 'multiply'. What I could figure out was that I had to add the location of my module to PYTHONPATH.

I couldn't find PYTHONPATH in System variables list, so I created one (as mentioned here), added all the paths including the one I needed, but it didn't solve the problem.

So, I re-installed all the features of Python, even the ones I didn't earlier. Everywhere I read solutions, there is only written set this like this or something similar but no-one says where they are defined or how they work.

Hence my question arises why can't I find variables like PYTHONPATH or PYTHONHOME in System variables while Python says they can be set or where are they defined?

Can anyone explain how do environment variables (mentioned in Python's documentation) work?

How can I solve my 'ImportError`?

Community
  • 1
  • 1
PalashV
  • 759
  • 3
  • 8
  • 25
  • What makes you think that the installer sets these variables? (It doesn't.) – kindall May 13 '16 at 21:24
  • Just because they can be set doesn't mean they are set. "How do environment variables work" isn't a Python question, and it's also a question you can probably answer yourself by searching the internet. – BrenBarn May 13 '16 at 21:24

1 Answers1

3

The installer might not have set those automatically. You can set them yourself in the command prompt if you like:

SET variableName = value

Or, if you use PowerShell:

$env:variableName = value

If you don't want to use system commands at all, it can be done from within python with the os module:

import os
os.environ[variableName] = value


If they are already set, but you want to add something to it, the process is a little different. Let's suppose the path you are trying to add to PYTHONPATH is path.

Using the command prompt:

SET PYTHONPATH=%PYTHONPATH%;path

Python:

import os
os.environ['PYTHONPATH'] = os.environ['PYTHONPATH'] + ";path"


If you are correct and this is happening because the module is not on PYTHONPATH, this should add it onto the path and fir the problem. If it doesn't fix the ImportError, then the environment variables are probably not the issue.

Links to learn more about environment variables:

  • http://www.digitalcitizen.life/simple-questions-what-are-environment-variables
  • https://en.wikipedia.org/wiki/Environment_variable
  • http://www.computerhope.com/jargon/e/envivari.htm

Hope this helps.

Ecko
  • 1,030
  • 9
  • 30
  • Don't use `os.putenv`. Use `os.environ[variableName] = value` – pppery May 13 '16 at 22:06
  • Is it bad practice or something? – Ecko May 13 '16 at 22:07
  • Changes to the environment made using `os.putenv` are not reflected in `os.environ`, which will confuse other python code reading the environment. – pppery May 13 '16 at 22:09
  • Thanks for letting me know. Is there something special about `os.environ` so that when it is changed it modifies the system values? – Ecko May 13 '16 at 22:10
  • 1
    Yes. `os.enivron` isn't an actual dictionary. It's a dictionary-like object which copies all changes to the environment. – pppery May 13 '16 at 22:12
  • @ppperry Can you tell me how to access or where `PYTHONPATH` is listed? Because when I run `SET` command from cmd, it isn't listed there. – PalashV May 13 '16 at 22:12
  • @PalashV the environment variable `PYTHONPATH` is not always defined. When `PYTHONPATH` is not defined or you use the `-E` flag, python will initialize `sys.path` with default values. – pppery May 13 '16 at 22:15
  • @ppperry Can I set these flags from Visual Studio 2015? – PalashV May 13 '16 at 22:16
  • @PalashV What I mean by the `-E` flag is running the python script as `python -E [other args]` – pppery May 13 '16 at 22:18
  • Can you add that to the answer? – Ecko May 13 '16 at 22:19
  • @ppperry Can you tell me from the question what am I missing? I mean do we need to manually set up env-vars, because I've seen everywhere how to change their values, but not where/how they are defined? Is there a need to setup the environment manually. – PalashV May 13 '16 at 22:22
  • @PalashV No, `PYTHON*` environment variables do not need to be set up to run python. – pppery May 13 '16 at 22:24
  • @ppperry I know there is some problem with my understanding but can you refer/guide me some source where I can read more the usage of env-vars in Py, apart from the reference from documentation. Or provide a solution to the `ImportError` so that I would be able to relate more what is happening, please. – PalashV May 13 '16 at 22:29
  • @XamuelSchulman: Hi Xamuel. I was trying to add `path` to `PYTHONPATH` from outside the Python. I had done what you wrote (through cmd) but it only created an env-var `PYTHONPATH` with value same as string literal `%PYTHONPATH%;path` (by using `SET` command), and it wasn't even getting mentioned in the list of `User variables` or `System variables`, so I thought `Python` would be initializing it. In the end, I added my file to the `PythonNN\` folder to run my project. I still don't have a clear understanding of how this all works, so I need to give it more time. Thanks for your time. – PalashV May 14 '16 at 16:54
  • 1
    @PalashV Well, I hope you can figure it out. Glad to be of help to you. ;) – Ecko May 14 '16 at 16:56