Environment / Configuration

All commands below are Windows OS specific

Tips & Notes

 

Quick Setup Venv

Open a Windows OS command prompt, navigate to an empty folder to store each Python virtual environment named 'venv'.  (Note: when you create each virtual environment, a subfolder will be created within the folder 'venv' for it)


cd \users\[your user name]\documents\[your python working folder]
md venv
cd venv

To see what Python versions are installed and where, enter the following from a command prompt:


py --list-paths

Update pip to the latest version:


py -m pip install --upgrade pip

Complete configuring the Python virtual environment named 'my_venv' for the latest Python 3x version (e.g. 'py -3') or a specific version (e.g. 'py -3.10') with venv


py -3 -m venv my_venv
cd my_venv
scripts\activate
py -V

The last command 'py -V' will show the current active Python version for the Python virtual environment.   Now use pip to install any other Python packages you will need for your Python virtual environment.  


py -m pip install [package name]

When you have all of the packages installed that you need, then build a requirements.text file (from within the virtual environment folder) using the 'freeze' command.   Run 'deactivate' to close the virtual environment.  


py -m pip list
py -m pip freeze > requirements.txt
scripts\deactivate
py -m pip list

 

The requirements.txt file makes it easy to share the current Python configuraion with someone else, and to pickup exactly where you left off (in terms of installed package versions).   To use the requirements.txt file, you navigate to the venv folder and then execute the pip 'install' command.   (no need to run the 'activate' command).  


py -m pip list
py -m pip install -r requirements.txt
py -m pip list

When you are all done with your Python session for the project, deactivate the virtual environment.  


py -m pip list
scripts\deactivate
py -m pip list

 

WARNING: Do not name any .py Python script files with a filename that matches the virtual environment folder name, any packages installed, or any Python commands.   If you do, you will experience run time errors that you cannot fix or explain.  

 

Uninstall all packages


py -m pip list
py -m pip freeze > requirements.txt
py -m pip uninstall -y -r requirements.txt
py -m pip list

Python Projects

Starting a Project


proj_py/
	proj_py/
	  __init__.py
	venv/
	setup.py
	requirements.txt
	LICENSE
	README.md

Ending a Project

Restarting a Project


cd proj_py
venv\scripts\activate
pip install -r requirements.txt
$ pip install . 

 

Virtual Environments

Example: To create a virtual environment within a specific folder for Flask, you would enter the commands below in a Windows command window.   A folder venv will be created in the 'proj-flask' folder for the virtual environment.  


mkdir proj_py
cd proj_py

py -3.5 -m venv venv

venv\scripts\activate

python -V

The last command python -V shows that Python version 3.5 is now available when the python command is executed.   It is a good idea to check and remove any unneeded modules:


pip -V
python -m pip install --upgrade pip

To see package dependencies, use the command:


py -3.5 -m pip show 

To unstall all package dependencies in a venv:


py -3.5 -m pipenv uninstall --all

Use the pip command pip list to see what packages are installed.   Use the command pip -version to see where new packages will be installed, and under what Python version.   Other pip commands include install and uninstall.  

When you have all of the packages installed that you need, then build a requirements.text file (from within the virtual environment) using the command:


pip freeze > requirements.txt

The requirements.txt file makes it easy to share the current Python configuraion with someone else, and to pickup exactly where you left off (in terms of installed package versions).   To use the requirements.txt file, you execute the command:


pip install -r requirements.txt

 

When you are all done with your Python session for the project, deactivate the virtual environment with the command:


venv\scripts\deactivate

 

The next time you wish to use the virtual environment, you only need to go to the project folder and use the command:


venv\scripts\activate

requirements.txt will recreate the working environment to run your code.   setup.py is for when a pip install is used on your package.  

 

__init__.py

Subfolders within a Python project folder should be used to organize files by functionality.   Adding the file __init__.py to a subfolder tells Python to treate the folder containing the files as a package.   Packages are a way of structuring Python's module namespace.   The file __init__.py can be empty, or it can hold initialization code.   Create a __init__.py file within every subfolder of your project to make the .py files accessible to a project script that uses the import command to access them.   You may also add import commands to the __init__.py file to cause them to be automatically imported simply by calling the parent folder name of __init__.py in an import statement.   Use lowercase and underscores for module/package names.  

More on packages: Py help   How to create a Python Package with __init__.py   python-packaging minimal structure   Python Modules: Creating, Importing, and Sharing   GeeksforGeeks   absolute and relative imports  

 

setup.py

setup.py is the build script for setuptools and contains a global setup() function.   It tells setuptools what code files to include in your package.  


#setup.py

from setuptools import setup

setup( 
	name='proj_py',
	version='0.1',
	description='describe thie project',
	url='http://github.com/proj/folder',
	long_description=long_description,
	author='Mechatronic Solutions LLC',
	author_email='mechtronsol@gmail.com',
	license='MIT',
	packages=['proj_py'],	#same as name
	zip_safe=False)

IMPORTANT: make sure name and packages correspond to the project folder name.  

Executing the command below will cause pip to use setup.py to install your module.  


$ pip install . 

More about setup.py   Packaging Python   Using setup.py   what-is-setup-py      

 

Modules

A module is a file containing Python definitions and statements with the file extension .py.   Within a module, the global variable __name__ has the value of "_main__" when the Python interpreter is running that module (.py file), otherwise if it is being impored from another module, then __name__ will be set to the module's name (.py filename).   Module filenames become variables/objects once imported, so choose them wisely.   Use lowercase and underscores for module/package names.  


if __name__ == "__main__": 
    print "Executed when invoked directly"
else: 
    print "Executed when imported"

Importing Modules

The command import module will retain the module's namespace, causing you to use the module name to access any of its functions or attributes (module.function).   Using the command from module import will import specific functions and attributes from a module into the calling module namespace, causing you to be able to reference them directly.  

Steve's python notes

 

Executing Particular Python Version

Windows installation since Python 3.3 installs py.exe and pyw.exe into the Windows folder and assigns the .py extension to use them as the default launcher.   Follow the command py with the argument -#.# where -#.# is the installed Python version you want to run.   Follow that with the script name to run a particular script.   To run script script.py with version 3.5 of Python:


py -3.5 -V
py -3.5 script.py

To update pip for a particular Python version:


py -3.5 -m pip install --upgrade pip

To install a particular package by Python version using pip:


py -3.5 -m pip install 

To uninstall a particular package by Python version using pip within venv, use the command pipenv


py -3.5 -m pipenv uninstall 
py -3.5 -m pipenv uninstall requirements.txt

To see package dependencies, use the command:


py -3.5 -m pip show 

To unstall all package dependencies in a venv:


py -3.5 -m pipenv uninstall --all