Python Forum
setting pythonpath variable in environment variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
setting pythonpath variable in environment variables
#1
parent/
    __init__.py
    one/
        __init__.py
        sample1.py
    two/
        __init__.py
        sample.py
  
I have a structure like above.The code works fine in PYCHARM IDE.To run the program through command prompt,I have set environment variables like PYTHON_HOME,PATH variables.Now when i run the code througb command prompt,it doesnt run like
<c:/users/parent>python two\sample.py.The error which i get is like "No module named one",as sample.py file has an import like "from one.sample1 import *" But when i set PYTHONPATH variable and run the code same way it runs.
PYTHONPATH is set as c:/users/parent
How can i understand this.?Is it correct way to run the code through command prompt?.Please help me to understand this
Reply
#2
Python imports modules. It means that when you write from one.sample1 import *, there is an importable module or package named one. Here are a few ways to achieve this:
  • Move the 'one' directory in 'two'. It will become importable in 'sample.py'
  • Put the directory containing 'one' on the python path by manipulating the python path through the PYTHONPATH environment variable or the files usercustomize.py or sitecustomize.py
  • Manipulate sys.path in 'sample.py' to make sure it contains the directory containing 'one'.
As you added an '__init__.py' file in 'parent', it may happen that you want 'one' or 'two' to be subpackages of module 'parent'. In that case you can put 'parent' on the python path instead of 'one' and just write from ..one.sample1 import * in 'sample.py' and invoke the script with the command python -m parent.two.sample.
Reply
#3
Thanks.I didnt understand third point that is about sys.path.And what is the difference between
 from ..one.sample1 import *
and
 from one.sample1 import *
Reply
#4
Quote:And what is the difference between
There are two hierarchies that don't coincide: the filesystem with its tree of folders and the python packages and subpackages system. For example xml.etree.ElementTree is a submodule of the xml.etree package which is a subpackage of xml.

The ..one means the subpackage 'one' of the current module's grandparent package. But you can't use it if you simply call python two/sample.py because when you do so the current module is __main__ which has no parent package nor grandparent package. If works if parent is a module with a subpackage two and a submodule sample, so it works if you call python -m parent.two.sample.

The filesystem tree helps to build python modules and packages, but there is no direct correspondance between them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to set PYTHONPATH in Visual Studio Code? aupres 5 3,775 Aug-15-2023, 03:51 PM
Last Post: snippsat
  Setting permanent user variables in Windows coder420 2 1,381 Jan-04-2022, 10:42 AM
Last Post: Larz60+
Question Calling on a Variable using other Variables jacknewport 4 1,956 Jul-23-2021, 04:18 PM
Last Post: jacknewport
  Create new variable dependent on two existing variables JoeOpdenaker 6 2,935 Oct-25-2020, 02:15 PM
Last Post: jefsummers
  Print variable values from a list of variables xnightwingx 3 2,575 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Help Setting Multiple Variables bzowk 0 1,575 Jul-18-2020, 06:59 PM
Last Post: bzowk
  the exe file by generated by pyinstaller ,can't get the PYTHONPATH roger2020 11 6,858 Jan-14-2020, 11:07 AM
Last Post: roger2020
  Trouble Setting a Variable True basing on an Imput VictorVictus 5 2,702 Aug-02-2019, 08:14 PM
Last Post: VictorVictus
  TESSDATA_PREFIX environment variable is set to your "tessdata" directory. rajeev1729 1 4,020 Jun-10-2019, 04:47 PM
Last Post: Gribouillis
  Help with environment path variables kuharido 0 1,858 Jun-07-2019, 09:18 PM
Last Post: kuharido

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020